C ා get host information

Get host information

Recently, we need to make a cooperation with the centralized control system to collect some information of each terminal equipment. We need to roughly collect the hardware information of the terminal equipment, CPU, memory, hard disk utilization and other information. After checking it on the Internet, WMI is the most convenient way to get this information. The implementation code is as follows:

 Models.PlatformInfo platformInfo = new Models.PlatformInfo();
string query = "Select * from {0}";
///Get operating system data
SelectQuery queryOS = new SelectQuery(string.Format(query, WindowAPIType.Win32_OperatingSystem));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(queryOS);
foreach (ManagementBaseObject os in searcher.Get())
{
    platformInfo.Version = Convert.ToString(os["Version"]);
    platformInfo.Caption = Convert.ToString(os["Caption"]);
    platformInfo.SerialNumber = Convert.ToString(os["SerialNumber"]);
    platformInfo.SystemDirectory = Convert.ToString(os["SystemDirectory"]);
    platformInfo.OSArchitecture = Convert.ToString(os["OSArchitecture"]);
    platformInfo.InstallDate = Convert.ToString(os["InstallDate"]);
    platformInfo.Organization = Convert.ToString(os["Organization"]);
}

You can use os.GetText(TextFormat.Mof); to get detailed information.
At present, the API interfaces that I can obtain are as follows:

/// <summary>
///Window Api name
/// </summary>
public enum WindowAPIType
{
    /// <summary>
    //Memory
    /// </summary>
    Win32_PhysicalMemory,
    /// <summary>
    /// CPU
    /// </summary>
    Win32_Processor,
    /// <summary>
    //Hard disk
    /// </summary>
    Win32_DiskDrive,
    /// <summary>
    ///Computer model
    /// </summary>
    Win32_ComputerSystemProduct,
    /// <summary>
    //Resolution
    /// </summary>
    Win32_DesktopMonitor,
    /// <summary>
    ///Video card details
    /// </summary>
    Win32_VideoController,
    /// <summary>
    ///Operating system
    /// </summary>
    Win32_OperatingSystem,
    /// <summary>
    ///Network adapter
    /// </summary>
    Win32_NetworkAdapter,
    /// <summary>
    ///Network adapter settings
    /// </summary>
    Win32_NetworkAdapterConfiguration,
    /// <summary>
    ///Operating system login
    /// </summary>
    Win32_ComputerSystem,
    /// <summary>
    //Keyboard
    /// </summary>
    Win32_Keyboard,
    /// <summary>
    ///Point input device, including mouse
    /// </summary>
    Win32_PointingDevice,
    /// <summary>
    //Motherboard
    /// </summary>
    Win32_BaseBoard,
    /// <summary>
    /// BIOS chip
    /// </summary>
    Win32_BIOS,
    /// <summary>
    ///Disc drive
    /// </summary>
    Win32_CDROMDrive,
    /// <summary>
    //Parallel port
    /// </summary>
    Win32_ParallelPort,
    /// <summary>
    //Serial port
    /// </summary>
    Win32_SerialPort,
    /// <summary>
    ///Serial port configuration
    /// </summary>
    Win32_SerialPortConfiguration,
    /// <summary>
    ///Multimedia, generally sound card
    /// </summary>
    Win32_SoundDevice,
    /// <summary>
    ///Main board slot (ISA & PCI & AGP)
    /// </summary>
    Win32_SystemSlot,
    /// <summary>
    ///USB controller
    /// </summary>
    Win32_USBController,
    /// <summary>
    //Printer
    /// </summary>
    Win32_Printer,
    /// <summary>
    ///Printer settings
    /// </summary>
    Win32_PrinterConfiguration,
    /// <summary>
    ///Printer tasks
    /// </summary>
    Win32_PrintJob,
    /// <summary>
    ///Printer port
    /// </summary>
    Win32_TCPIPPrinterPort,
    /// <summary>
    /// Modem
    /// </summary>
    Win32_POTSModem,
    /// <summary>
    ///MODEM port
    /// </summary>
    Win32_POTSModemToSerialPort,
    /// <summary>
    //Graphics card
    /// </summary>
    Win32_DisplayConfiguration,
    /// <summary>
    ///Video card settings
    /// </summary>
    Win32_DisplayControllerConfiguration,
    /// <summary>
    ///Display support display mode
    /// </summary>
    Win32_VideoSettings,
    /// <summary>
    //Time zone
    /// </summary>
    Win32_TimeZone,
    /// <summary>
    ///Driver
    /// </summary>
    Win32_SystemDriver,
    /// <summary>
    ///Partition
    /// </summary>
    Win32_DiskPartition,
    /// <summary>
    ///Logical disk
    /// </summary>
    Win32_LogicalDisk,
    /// <summary>
    ///Partition and beginning and end location of logical disk
    /// </summary>
    Win32_LogicalDiskToPartition,
    /// <summary>
    ///Logical memory configuration
    /// </summary>
    Win32_LogicalMemoryConfiguration,
    /// <summary>
    ///System page file information
    /// </summary>
    Win32_PageFile,
    /// <summary>
    ///Page file settings
    /// </summary>
    Win32_PageFileSetting,
    /// <summary>
    ///System startup configuration
    /// </summary>
    Win32_BootConfiguration,
    /// <summary>
    ///System auto start program
    /// </summary>
    Win32_StartupCommand,
    /// <summary>
    ///Services for system installation
    /// </summary>
    Win32_Service,
    /// <summary>
    ///System management group
    /// </summary>
    Win32_Group,
    /// <summary>
    ///System group account
    /// </summary>
    Win32_GroupUser,
    /// <summary>
    ///User account
    /// </summary>
    Win32_UserAccount,
    /// <summary>
    ///System process
    /// </summary>
    Win32_Process,
    /// <summary>
    ///System thread
    /// </summary>
    Win32_Thread,
    /// <summary>
    //Sharing
    /// </summary>
    Win32_Share,
    /// <summary>
    ///Installed network clients
    /// </summary>
    Win32_NetworkClient,
    /// <summary>
    ///Installed network protocol
    /// </summary>
    Win32_NetworkProtocol,
}

All queries are consistent: "Select * from {0}"
In this way, you can get the information you want to get.
Get power on time:

/// <summary>
///Get power on time
/// </summary>
/// <returns></returns>
public string GetRunTime()
{
    TimeSpan ts = TimeSpan.FromMilliseconds(Environment.TickCount);
    return ts.ToString();
}

Get CPU usage using the performance counter component.

private PerformanceCounter performance = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);

this.performance.MachineName = ".";
this.performance.NextValue();

/// <summary>
///CPU usage
/// </summary>
/// <returns></returns>
public string GetCPU()
{
    return this.performance.NextValue().ToString("F1") + "%";
}

Tags: Python network

Posted on Sat, 02 Nov 2019 16:47:12 -0700 by BlueBoden