C#獲取主機信息

獲取主機信息

最近須要作一個配合集控系統收集各個終端設備的一些信息,大體須要收集終端設備的硬件信息,CPU、內存以及硬盤使用率等信息。網上查看了一番,使用WMI來獲取這些信息是最方便的。實現代碼以下:

Models.PlatformInfo platformInfo = new Models.PlatformInfo();
string query = "Select * from {0}";
///獲取操做系統數據
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"]);
}

其中能夠使用os.GetText(TextFormat.Mof);來獲取詳細的信息。
目前我查到的能夠獲取的API接口有以下:c#

/// <summary>
/// Window Api名稱
/// </summary>
public enum WindowAPIType
{
    /// <summary>
    /// 內存
    /// </summary>
    Win32_PhysicalMemory,
    /// <summary>
    /// CPU
    /// </summary>
    Win32_Processor,
    /// <summary>
    /// 硬盤
    /// </summary>
    Win32_DiskDrive,
    /// <summary>
    /// 電腦型號
    /// </summary>
    Win32_ComputerSystemProduct,
    /// <summary>
    /// 分辨率
    /// </summary>
    Win32_DesktopMonitor,
    /// <summary>
    /// 顯卡細節
    /// </summary>
    Win32_VideoController,
    /// <summary>
    /// 操做系統
    /// </summary>
    Win32_OperatingSystem,
    /// <summary>
    /// 網絡適配器
    /// </summary>
    Win32_NetworkAdapter,
    /// <summary>
    /// 網絡適配器設置
    /// </summary>
    Win32_NetworkAdapterConfiguration,
    /// <summary>
    /// 操做系統登陸
    /// </summary>
    Win32_ComputerSystem,
    /// <summary>
    /// 鍵盤
    /// </summary>
    Win32_Keyboard,
    /// <summary>
    /// 點輸入設備,包括鼠標
    /// </summary>
    Win32_PointingDevice,
    /// <summary>
    /// 主板
    /// </summary>
    Win32_BaseBoard,
    /// <summary>
    /// BIOS芯片
    /// </summary>
    Win32_BIOS,
    /// <summary>
    /// 光盤驅動盤
    /// </summary>
    Win32_CDROMDrive,
    /// <summary>
    /// 並口
    /// </summary>
    Win32_ParallelPort,
    /// <summary>
    /// 串口
    /// </summary>
    Win32_SerialPort,
    /// <summary>
    /// 串口配置
    /// </summary>
    Win32_SerialPortConfiguration,
    /// <summary>
    /// 多媒體,通常指聲卡
    /// </summary>
    Win32_SoundDevice,
    /// <summary>
    /// 主板插槽(ISA&PCI&AGP)
    /// </summary>
    Win32_SystemSlot,
    /// <summary>
    /// USB控制器
    /// </summary>
    Win32_USBController,
    /// <summary>
    /// 打印機
    /// </summary>
    Win32_Printer,
    /// <summary>
    /// 打印機設置
    /// </summary>
    Win32_PrinterConfiguration,
    /// <summary>
    /// 打印機任務
    /// </summary>
    Win32_PrintJob,
    /// <summary>
    /// 打印機端口
    /// </summary>
    Win32_TCPIPPrinterPort,
    /// <summary>
    /// Modem
    /// </summary>
    Win32_POTSModem,
    /// <summary>
    /// MODEM端口
    /// </summary>
    Win32_POTSModemToSerialPort,
    /// <summary>
    /// 顯卡
    /// </summary>
    Win32_DisplayConfiguration,
    /// <summary>
    /// 顯卡設置
    /// </summary>
    Win32_DisplayControllerConfiguration,
    /// <summary>
    /// 顯示支持顯示模式
    /// </summary>
    Win32_VideoSettings,
    /// <summary>
    /// 時區
    /// </summary>
    Win32_TimeZone,
    /// <summary>
    /// 驅動程序
    /// </summary>
    Win32_SystemDriver,
    /// <summary>
    /// 磁盤分區
    /// </summary>
    Win32_DiskPartition,
    /// <summary>
    /// 邏輯磁盤
    /// </summary>
    Win32_LogicalDisk,
    /// <summary>
    /// 邏輯磁盤所在分區及始末位置
    /// </summary>
    Win32_LogicalDiskToPartition,
    /// <summary>
    /// 邏輯內存配置
    /// </summary>
    Win32_LogicalMemoryConfiguration,
    /// <summary>
    /// 系統頁文件信息
    /// </summary>
    Win32_PageFile,
    /// <summary>
    /// 頁文件設置
    /// </summary>
    Win32_PageFileSetting,
    /// <summary>
    /// 系統啓動配置
    /// </summary>
    Win32_BootConfiguration,
    /// <summary>
    /// 系統自動啓動程序
    /// </summary>
    Win32_StartupCommand,
    /// <summary>
    /// 系統安裝的服務
    /// </summary>
    Win32_Service,
    /// <summary>
    /// 系統管理組
    /// </summary>
    Win32_Group,
    /// <summary>
    /// 系統組帳號
    /// </summary>
    Win32_GroupUser,
    /// <summary>
    /// 用戶帳戶
    /// </summary>
    Win32_UserAccount,
    /// <summary>
    /// 系統進程
    /// </summary>
    Win32_Process,
    /// <summary>
    /// 系統線程
    /// </summary>
    Win32_Thread,
    /// <summary>
    /// 共享
    /// </summary>
    Win32_Share,
    /// <summary>
    /// 已安裝的網絡客戶端
    /// </summary>
    Win32_NetworkClient,
    /// <summary>
    /// 已安裝的網絡協議
    /// </summary>
    Win32_NetworkProtocol,
}

查詢都是一致的:"Select * from {0}"
這樣就能獲取到想要獲取到的信息了。
獲取開機時間:網絡

/// <summary>
/// 獲取開機運行時間
/// </summary>
/// <returns></returns>
public string GetRunTime()
{
    TimeSpan ts = TimeSpan.FromMilliseconds(Environment.TickCount);
    return ts.ToString();
}

獲取CPU使用率能夠使用性能計數器組件。ide

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

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

/// <summary>
/// CPU使用率
/// </summary>
/// <returns></returns>
public string GetCPU()
{
    return this.performance.NextValue().ToString("F1") + "%";
}
相關文章
相關標籤/搜索