获取计算机的信息(IP地址、MAC地址、CUP序列号、硬盘序列号、主板信息等等)
2008-09-03 12:00
639 查看
1、Windows Management Instrumentation(WMI)提供了获取信息的方法,在C#中可用通过System.Management命名空间中的类访问。比如获取CPU ID的方法:
string GetCpuID()
{
try
{
string id = "";
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
id = mo.Properties["ProcessorId"].Value.ToString();
}
return id;
}
catch
{
return "unknow";
}
}
2、那么像“Win32_Processor”、“ProcessorId”一类的字符串该如何写?这里有详细说明:
http://msdn.microsoft.com/en-us/library/aa394084(VS.85).aspx
比如主板信息的Win32_BaseBoard类:
class Win32_BaseBoard : CIM_Card
{
string Caption;
string ConfigOptions[];
string CreationClassName;
real32 Depth;
string Description;
real32 Height;
boolean HostingBoard;
boolean HotSwappable;
datetime InstallDate;
string Manufacturer;
string Model;
string Name;
string OtherIdentifyingInfo;
string PartNumber;
boolean PoweredOn;
string Product;
boolean Removable;
boolean Replaceable;
string RequirementsDescription;
boolean RequiresDaughterBoard;
string SerialNumber;
string SKU;
string SlotLayout;
boolean SpecialRequirements;
string Status;
string Tag;
string Version;
real32 Weight;
real32 Width;
};
如果我们想知道主板的SerialNumber,参照Win32_BaseBoard类的红色文字,我们就可以写出如下代码:
string GetMotherBoardSerialNumber()
{
try
{
string sn = "";
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
sn = mo.Properties["SerialNumber"].Value.ToString();
}
return sn;
}
catch
{
return "unknow";
}
}
3、例子:
注释部分是在测试机器上得到的值。
private void CpuInfo()
{
try
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
UInt16 AddressWidth = (UInt16)(mo.Properties["AddressWidth"].Value);//32
UInt16 DataWidth = (UInt16)(mo.Properties["DataWidth"].Value);//32
UInt32 CurrentClockSpeed = (UInt32)(mo.Properties["CurrentClockSpeed"].Value);//2810
UInt32 MaxClockSpeed = (UInt32)(mo.Properties["MaxClockSpeed"].Value);//2810
UInt32 ExtClock = (UInt32)(mo.Properties["ExtClock"].Value);//200
string Manufacturer = mo.Properties["Manufacturer"].Value.ToString();//GenuineIntel
string Caption = mo.Properties["Caption"].Value.ToString();//x86 Family 15 Model 4 Stepping 7
string Name = mo.Properties["Name"].Value.ToString();//Intel(R) Pentium(R) D CPU 2.80GHz
string SocketDesignation = mo.Properties["SocketDesignation"].Value.ToString();//socket775
}
}
catch (Exception ex)
{
Console.WriteLine("unknow");
}
}
private void GetMotherBoardInfo()
{
try
{
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
string Manufacturer = mo.Properties["Manufacturer"].Value.ToString();//ASUSTeK Computer INC.
string Name = mo.Properties["Name"].Value.ToString();//底板
string Product = mo.Properties["Product"].Value.ToString();//P5PL2
string SlotLayout = mo.Properties["SlotLayout"].Value.ToString();//在测试机器上没有得到该信息
string Version = mo.Properties["Version"].Value.ToString();//Rev 1.xx
}
}
catch(Exception ex)
{
Console.WriteLine("unknow");
}
}
private void DiskInfo()
{
try
{
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
string InterfaceType = mo.Properties["InterfaceType"].Value.ToString();//IDE
string Model = mo.Properties["Model"].Value.ToString();//MAXTOR 6L080J4
string Name = mo.Properties["Name"].Value.ToString();//\\.\PHYSICALDRIVE0
UInt32 Partitions = (UInt32)(mo.Properties["Partitions"].Value);//1
UInt64 Size = (UInt64)(mo.Properties["Size"].Value);//80048424960
UInt64 TotalCylinders = (UInt64)(mo.Properties["TotalCylinders"].Value);//9732
UInt32 TotalHeads = (UInt32)(mo.Properties["TotalHeads"].Value);//255
UInt64 TotalSectors = (UInt64)(mo.Properties["TotalSectors"].Value);//156344580
UInt64 TotalTracks = (UInt64)(mo.Properties["TotalTracks"].Value);//2481660
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
4、用WMI获得信息也存在一个小问题,就是比较慢,性能不好。如果对性能有要求,就要通过平台调用的方式调用Win32相关函数。比如通过下面函数可得到物理内存大小等信息:
[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MemoryInfo memInfo);
5、其他参考:
/article/6157817.html
string GetCpuID()
{
try
{
string id = "";
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
id = mo.Properties["ProcessorId"].Value.ToString();
}
return id;
}
catch
{
return "unknow";
}
}
2、那么像“Win32_Processor”、“ProcessorId”一类的字符串该如何写?这里有详细说明:
http://msdn.microsoft.com/en-us/library/aa394084(VS.85).aspx
比如主板信息的Win32_BaseBoard类:
class Win32_BaseBoard : CIM_Card
{
string Caption;
string ConfigOptions[];
string CreationClassName;
real32 Depth;
string Description;
real32 Height;
boolean HostingBoard;
boolean HotSwappable;
datetime InstallDate;
string Manufacturer;
string Model;
string Name;
string OtherIdentifyingInfo;
string PartNumber;
boolean PoweredOn;
string Product;
boolean Removable;
boolean Replaceable;
string RequirementsDescription;
boolean RequiresDaughterBoard;
string SerialNumber;
string SKU;
string SlotLayout;
boolean SpecialRequirements;
string Status;
string Tag;
string Version;
real32 Weight;
real32 Width;
};
如果我们想知道主板的SerialNumber,参照Win32_BaseBoard类的红色文字,我们就可以写出如下代码:
string GetMotherBoardSerialNumber()
{
try
{
string sn = "";
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
sn = mo.Properties["SerialNumber"].Value.ToString();
}
return sn;
}
catch
{
return "unknow";
}
}
3、例子:
注释部分是在测试机器上得到的值。
private void CpuInfo()
{
try
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
UInt16 AddressWidth = (UInt16)(mo.Properties["AddressWidth"].Value);//32
UInt16 DataWidth = (UInt16)(mo.Properties["DataWidth"].Value);//32
UInt32 CurrentClockSpeed = (UInt32)(mo.Properties["CurrentClockSpeed"].Value);//2810
UInt32 MaxClockSpeed = (UInt32)(mo.Properties["MaxClockSpeed"].Value);//2810
UInt32 ExtClock = (UInt32)(mo.Properties["ExtClock"].Value);//200
string Manufacturer = mo.Properties["Manufacturer"].Value.ToString();//GenuineIntel
string Caption = mo.Properties["Caption"].Value.ToString();//x86 Family 15 Model 4 Stepping 7
string Name = mo.Properties["Name"].Value.ToString();//Intel(R) Pentium(R) D CPU 2.80GHz
string SocketDesignation = mo.Properties["SocketDesignation"].Value.ToString();//socket775
}
}
catch (Exception ex)
{
Console.WriteLine("unknow");
}
}
private void GetMotherBoardInfo()
{
try
{
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
string Manufacturer = mo.Properties["Manufacturer"].Value.ToString();//ASUSTeK Computer INC.
string Name = mo.Properties["Name"].Value.ToString();//底板
string Product = mo.Properties["Product"].Value.ToString();//P5PL2
string SlotLayout = mo.Properties["SlotLayout"].Value.ToString();//在测试机器上没有得到该信息
string Version = mo.Properties["Version"].Value.ToString();//Rev 1.xx
}
}
catch(Exception ex)
{
Console.WriteLine("unknow");
}
}
private void DiskInfo()
{
try
{
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
string InterfaceType = mo.Properties["InterfaceType"].Value.ToString();//IDE
string Model = mo.Properties["Model"].Value.ToString();//MAXTOR 6L080J4
string Name = mo.Properties["Name"].Value.ToString();//\\.\PHYSICALDRIVE0
UInt32 Partitions = (UInt32)(mo.Properties["Partitions"].Value);//1
UInt64 Size = (UInt64)(mo.Properties["Size"].Value);//80048424960
UInt64 TotalCylinders = (UInt64)(mo.Properties["TotalCylinders"].Value);//9732
UInt32 TotalHeads = (UInt32)(mo.Properties["TotalHeads"].Value);//255
UInt64 TotalSectors = (UInt64)(mo.Properties["TotalSectors"].Value);//156344580
UInt64 TotalTracks = (UInt64)(mo.Properties["TotalTracks"].Value);//2481660
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
4、用WMI获得信息也存在一个小问题,就是比较慢,性能不好。如果对性能有要求,就要通过平台调用的方式调用Win32相关函数。比如通过下面函数可得到物理内存大小等信息:
[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MemoryInfo memInfo);
5、其他参考:
/article/6157817.html
相关文章推荐
- 获取计算机的信息(IP地址、MAC地址、CUP序列号、硬盘序列号、主板信息等等)
- 超强获取计算机信息类(可获取cpu序列号,网卡地址,ip地址,硬盘id,登录系统用户,计算机名等等)
- 超强获取计算机信息类(可获取cpu序列号,网卡地址,ip地址,硬盘id,登录系统用户,计算机名等等)
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存 【转载】
- 计算机硬件通用功能类:硬件信息控制器(主机名,cpu编号,网卡地址,MAC地址,主硬盘编号,ip地址,获取最大线程数,验证服务IP)
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存 【转载】
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- C#中获取CPU序列号、网卡Mac地址、IP地址、硬盘序列号、登录用户名、PC类型、计算机名称、物理内存
- C#获取CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型
- c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- C# 获取本机CPU序列号,MAC地址,硬盘ID,本机IP地址,计算机名,物理内存,PC类型
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- C#中获取CPU序列号、网卡Mac地址、IP地址、硬盘序列号、登录用户名、PC类型、计算机名称、物理内存,Singleton模式
- [收藏]如何获取机器的硬件信息(CPU ID序列号, 主板信息,硬盘序列号,系统信息,等等)
- C#中获取CPU序列号、网卡Mac地址、IP地址、硬盘序列号、登录用户名、PC类型、计算机名称、物理内存