.NET基础示例系列之十三:WMI获取硬件信息
欲了解更详细的信息,可查找MSDN——WIN32和COM开发——Administration and Management——Windows Management Instrumentation (WMI)——SDK Documentation——Windows Management Instrumentation——WMI Reference——WMI Classes——Win32 Classes
或MSDN网址:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_reference.asp
使用WMI获取这些系统信息是不太有保证的,例如你会发现,并不是每个win32类中的属性都能成功在获得,不少如主板序列号这样的属性常常是null或空串。
//主板信息:
public static void GetBaseBoardInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_BaseBoard");
foreach (ManagementObject mo in searcher.Get())
{
// mo["Manufacturer"],制造商,如“On-data”(昂达)
// mo["Product"],型号,如“KT400A-8235”
// mo["SerialNumber"],序列号
}
}
catch
{
}
}
//BIOS信息:
public static void GetBiosInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_BIOS");
foreach (ManagementObject mo in searcher.Get())
{
// mo["Manufacturer"],制造商,如“Phoenix Technologies, LTD”
// mo["SerialNumber"],序列号
}
}
catch
{
}
}
//物理磁盘信息:
public static void GetPhysicalDiskInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_DiskDrive");
foreach (ManagementObject mo in searcher.Get())
{
// mo["MediaType"],类型,如“Fixed hard disk media”
// mo["SizeBytes "],容量,如“80023749120”(字节)
}
}
catch
{
}
}
//网卡配置信息:
public static void GetNAConfigurationInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_NetworkAdapterConfiguration");
foreach (ManagementObject mo in searcher.Get())
{
// mo["IPAddress"],IP地址
// mo["DefaultIPGateway"],默认网关
// mo["DNSServerSearchOrder"],DNS地址,如果有备用则会以数组形式返回
// mo["IPSubnet"],子网掩码
// mo["MACAddress"],MAC地址
}
}
catch
{
}
}
//CPU信息:
public static void GetProcessorInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_Processor");
foreach (ManagementObject mo in searcher.Get())
{
// mo["L2CacheSize"],二级缓存,如“512”
// mo["Manufacturer"],制造商,如“AuthenticAMD”
// mo["MaxClockSpeed"],主频,如“1824”(1.824G)
// mo["ProcessorId"],ID,如“0383F9FF000006A0”
}
}
catch
{
}
}
//逻辑磁盘信息:
public static void GetLogicalDiskInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_LogicalDisk");
foreach (ManagementObject mo in searcher.Get())
{
// mo["FileSystem"],文件系统,如“FAT32”
// mo["FreeSpace"],剩余空间,如“4554891264”(4G多)
// mo["Name"],卷标,如“C:”
// mo["Size"],大小,如“10476945408”(10G)
}
}
catch
{
}
}
//进程信息:
public static void GetProcessInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_Process");
ManagementOperationObserver observer = new ManagementOperationObserver();
ObjectReadyHandler handler = new ObjectReadyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(handler.Done);
foreach (ManagementObject mo in searcher.Get())
{
// mo["ExecutablePath"],进程执行文件的路径,如“C:\WINDOWS\system32\winlogon.exe”
// mo["Name"],进程名称,如“WINLOGON.EXE”
// mo["ParentProcessId"],父进程ID
// mo["ProcessId"],进程ID
//以下代码获得进程的拥有者
mo.InvokeMethod(observer, "GetOwner", null);
while (!handler.OperationCompleted)
{
System.Threading.Thread.Sleep(50);
}
if (handler.ReturnMbo["returnValue"].ToString() == "0")
{
//handler.ReturnMbo["User"].ToString(),进程拥有者
}
}
}
catch
{
}
}
public class ObjectReadyHandler
{
private ManagementBaseObject returnMbo;
private bool operationCompleted = false;
public void Done(object sender, ObjectReadyEventArgs e)
{
this.operationCompleted = true;
this.returnMbo = e.NewObject;
}
public ManagementBaseObject ReturnMbo
{
get
{
return this.returnMbo;
}
}
public bool OperationCompleted
{
get
{
return this.operationCompleted;
}
}
}
//服务信息:
public static void GetServiceInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_Service");
foreach (ManagementObject mo in searcher.Get())
{
// mo["Name"],服务名称,如“Alerter”
// mo["PathName"],文件路径,如“C:\WINDOWS\system32\svchost.exe -k LocalService”
// mo["Started"],是否已启动
// mo["StartMode"],启动类型,包括"Boot"、"System"、"Auto"、"Manual"、"Disabled"
}
}
catch
{
}
}
//内存信息:
public static void GetMemoryInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From CIM_OperatingSystem");
foreach (ManagementObject mo in searcher.Get())
{
// mo["FreePhysicalMemory"],剩余物理内存量
// mo["FreeVirtualMemory"],剩余虚拟内存量
// mo["TotalVirtualMemorySize"],总虚拟内存量
// mo["TotalVisibleMemorySize"],总物理内存量
}
}
catch
{
}
}
public static void GetBaseBoardInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_BaseBoard");
foreach (ManagementObject mo in searcher.Get())
{
// mo["Manufacturer"],制造商,如“On-data”(昂达)
// mo["Product"],型号,如“KT400A-8235”
// mo["SerialNumber"],序列号
}
}
catch
{
}
}
//BIOS信息:
public static void GetBiosInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_BIOS");
foreach (ManagementObject mo in searcher.Get())
{
// mo["Manufacturer"],制造商,如“Phoenix Technologies, LTD”
// mo["SerialNumber"],序列号
}
}
catch
{
}
}
//物理磁盘信息:
public static void GetPhysicalDiskInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_DiskDrive");
foreach (ManagementObject mo in searcher.Get())
{
// mo["MediaType"],类型,如“Fixed hard disk media”
// mo["SizeBytes "],容量,如“80023749120”(字节)
}
}
catch
{
}
}
//网卡配置信息:
public static void GetNAConfigurationInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_NetworkAdapterConfiguration");
foreach (ManagementObject mo in searcher.Get())
{
// mo["IPAddress"],IP地址
// mo["DefaultIPGateway"],默认网关
// mo["DNSServerSearchOrder"],DNS地址,如果有备用则会以数组形式返回
// mo["IPSubnet"],子网掩码
// mo["MACAddress"],MAC地址
}
}
catch
{
}
}
//CPU信息:
public static void GetProcessorInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_Processor");
foreach (ManagementObject mo in searcher.Get())
{
// mo["L2CacheSize"],二级缓存,如“512”
// mo["Manufacturer"],制造商,如“AuthenticAMD”
// mo["MaxClockSpeed"],主频,如“1824”(1.824G)
// mo["ProcessorId"],ID,如“0383F9FF000006A0”
}
}
catch
{
}
}
//逻辑磁盘信息:
public static void GetLogicalDiskInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_LogicalDisk");
foreach (ManagementObject mo in searcher.Get())
{
// mo["FileSystem"],文件系统,如“FAT32”
// mo["FreeSpace"],剩余空间,如“4554891264”(4G多)
// mo["Name"],卷标,如“C:”
// mo["Size"],大小,如“10476945408”(10G)
}
}
catch
{
}
}
//进程信息:
public static void GetProcessInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_Process");
ManagementOperationObserver observer = new ManagementOperationObserver();
ObjectReadyHandler handler = new ObjectReadyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(handler.Done);
foreach (ManagementObject mo in searcher.Get())
{
// mo["ExecutablePath"],进程执行文件的路径,如“C:\WINDOWS\system32\winlogon.exe”
// mo["Name"],进程名称,如“WINLOGON.EXE”
// mo["ParentProcessId"],父进程ID
// mo["ProcessId"],进程ID
//以下代码获得进程的拥有者
mo.InvokeMethod(observer, "GetOwner", null);
while (!handler.OperationCompleted)
{
System.Threading.Thread.Sleep(50);
}
if (handler.ReturnMbo["returnValue"].ToString() == "0")
{
//handler.ReturnMbo["User"].ToString(),进程拥有者
}
}
}
catch
{
}
}
public class ObjectReadyHandler
{
private ManagementBaseObject returnMbo;
private bool operationCompleted = false;
public void Done(object sender, ObjectReadyEventArgs e)
{
this.operationCompleted = true;
this.returnMbo = e.NewObject;
}
public ManagementBaseObject ReturnMbo
{
get
{
return this.returnMbo;
}
}
public bool OperationCompleted
{
get
{
return this.operationCompleted;
}
}
}
//服务信息:
public static void GetServiceInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From Win32_Service");
foreach (ManagementObject mo in searcher.Get())
{
// mo["Name"],服务名称,如“Alerter”
// mo["PathName"],文件路径,如“C:\WINDOWS\system32\svchost.exe -k LocalService”
// mo["Started"],是否已启动
// mo["StartMode"],启动类型,包括"Boot"、"System"、"Auto"、"Manual"、"Disabled"
}
}
catch
{
}
}
//内存信息:
public static void GetMemoryInfo()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select * From CIM_OperatingSystem");
foreach (ManagementObject mo in searcher.Get())
{
// mo["FreePhysicalMemory"],剩余物理内存量
// mo["FreeVirtualMemory"],剩余虚拟内存量
// mo["TotalVirtualMemorySize"],总虚拟内存量
// mo["TotalVisibleMemorySize"],总物理内存量
}
}
catch
{
}
}
关注作者:欢迎扫码关注公众号「后厂村思维导图馆」,获取本人自建的免费ChatGPT跳板地址,长期有效。 原文链接:https://www.cnblogs.com/morvenhuang/archive/2006/11/25/572281.html 版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。 |