使用System.Management
类获取系统信息:
using System.Management;
namespace test_cpuid
{
class Program
{
static private void GetInfo()
{
string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Response.Write("cpu序列号:" + cpuInfo.ToString());
}
//获取硬盘ID
String HDid;
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
Response.Write("硬盘序列号:" + HDid.ToString());
}
//获取网卡硬件地址
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if ((bool)mo["IPEnabled"] == true)
Response.Write("MAC address\t{0}" + mo["MacAddress"].ToString());
mo.Dispose();
}
}
static void Main(string[] args)
{
GetInfo();
}
}
}
/// 显卡PNPDeviceID
public string GetVideoPNPID()
{
string st = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_VideoController");
foreach (ManagementObject mo in mos.Get())
{
st = mo["PNPDeviceID"].ToString();
}
return st;
}
/// 声卡PNPDeviceID
public string GetSoundPNPID()
{
string st = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
foreach (ManagementObject mo in mos.Get())
{
st = mo["PNPDeviceID"].ToString();
}
return st;
}
/// CPU版本信息
public string GetCPUVersion()
{
string st = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (ManagementObject mo in mos.Get())
{
st = mo["Version"].ToString();
}
return st;
}
/// CPU名称信息
public string GetCPUName()
{
string st = "";
ManagementObjectSearcher driveID = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (ManagementObject mo in driveID.Get())
{
st = mo["Name"].ToString();
}
return st;
}
/// CPU制造厂商
public string GetCPUManufacturer()
{
string st = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (ManagementObject mo in mos.Get())
{
st = mo["Manufacturer"].ToString();
}
return st;
}
/// 主板制造厂商
public string GetBoardManufacturer()
{
SelectQuery query = new SelectQuery("Select * from Win32_BaseBoard");
ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
ManagementObjectCollection.ManagementObjectEnumerator data = mos.Get().GetEnumerator();
data.MoveNext();
ManagementBaseObject board = data.Current;
return board.GetPropertyValue("Manufacturer").ToString();
}
/// 主板编号
public string GetBoardID()
{
string st = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
foreach (ManagementObject mo in mos.Get())
{
st = mo["SerialNumber"].ToString();
}
return st;
}
/// 主板型号
public string GetBoardType()
{
string st = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
foreach (ManagementObject mo in mos.Get())
{
st = mo["Product"].ToString();
}
return st;
}
可供引用的WMI类库在MSDN中可查到::http://msdn.microsoft.com/en-us/library/aa394084(v=VS.85).aspx
public enum WMIPath
{
// 硬件
Win32_Processor, // CPU 处理器
Win32_PhysicalMemory, // 物理内存条
Win32_Keyboard, // 键盘
Win32_PointingDevice, // 点输入设备,包括鼠标。
Win32_FloppyDrive, // 软盘驱动器
Win32_DiskDrive, // 硬盘驱动器
Win32_CDROMDrive, // 光盘驱动器
Win32_BaseBoard, // 主板
Win32_BIOS, // BIOS 芯片
Win32_ParallelPort, // 并口
Win32_SerialPort, // 串口
Win32_SerialPortConfiguration, // 串口配置
Win32_SoundDevice, // 多媒体设置,一般指声卡。
Win32_SystemSlot, // 主板插槽(ISA & PCI & AGP)
Win32_USBController, // USB 控制器
Win32_NetworkAdapter, // 网络适配器
Win32_NetworkAdapterConfiguration, // 网络适配器设置
Win32_Printer, // 打印机
Win32_PrinterConfiguration, // 打印机设置
Win32_PrintJob, // 打印机任务
Win32_TCPIPPrinterPort, // 打印机端口
Win32_POTSModem, // MODEM
Win32_POTSModemToSerialPort, // MODEM 端口
Win32_DesktopMonitor, // 显示器
Win32_DisplayConfiguration, // 显卡
Win32_DisplayControllerConfiguration, // 显卡设置
Win32_VideoController, // 显卡细节。
Win32_VideoSettings, // 显卡支持的显示模式。
// 操作系统
Win32_TimeZone, // 时区
Win32_SystemDriver, // 驱动程序
Win32_DiskPartition, // 磁盘分区
Win32_LogicalDisk, // 逻辑磁盘
Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
Win32_LogicalMemoryConfiguration, // 逻辑内存配置
Win32_PageFile, // 系统页文件信息
Win32_PageFileSetting, // 页文件设置
Win32_BootConfiguration, // 系统启动配置
Win32_ComputerSystem, // 计算机信息简要
Win32_OperatingSystem, // 操作系统信息
Win32_StartupCommand, // 系统自动启动程序
Win32_Service, // 系统安装的服务
Win32_Group, // 系统管理组
Win32_GroupUser, // 系统组帐号
Win32_UserAccount, // 用户帐号
Win32_Process, // 系统进程
Win32_Thread, // 系统线程
Win32_Share, // 共享
Win32_NetworkClient, // 已安装的网络客户端
Win32_NetworkProtocol, // 已安装的网络协议
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
// restart remote computer
public
static
string
ShutDownComputer(
string
strHost,
string
strUserName,
string
strPassWrod)
{
try
{
ConnectionOptions Options =
new
ConnectionOptions();
// 定义连接远程计算机的一些选项
Options.Username = strUserName;
Options.Password = strPassWrod;
//远程计算机名或IP地址
ManagementScope Scope =
new
ManagementScope(
string
.Format(
"\\\\{0}\\root\\cimv2"
,strHost),Options);
Scope.Connect();
// 用给定管理者用户名和口令连接远程的计算机
System.Management.ObjectQuery oq =
new
System.Management.ObjectQuery(
"SELECT * FROM Win32_OperatingSystem"
);
ManagementObjectSearcher Query =
new
ManagementObjectSearcher(Scope,oq);
ManagementObjectCollection QueryCollection = Query.Get();
// 得到WMI控制
foreach
(ManagementObject mo
in
QueryCollection)
{
mo.InvokeMethod(
"Reboot"
,
new
string
[]{
""
});
// 重启远程计算机
}
return
null
;
}
catch
(Exception me)
{
return
me.Message;
}
}
//获取磁盘信息
public string GetDiskInfo
{
string str = "" ;
SelectQuery query = new SelectQuery("Select * From Win32_LogicalDisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementBaseObject disk in searcher.Get())
{
str = disk["Name"] +" "+disk["DriveType"] + " " + disk["VolumeName"] ;
Console.WriteLine("\r\n"+str) ;
}
Console.ReadLine();
// get disk size
ManagementObject disk = new ManagementObject( "win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.ReadLine(); return str ;
}
disk["DriveType"] 的返回值意义如下:
1 No type
2 Floppy disk
3 Hard disk
4 Removable drive or network drive
5 CD-ROM
6 RAM disk
//列出机器中所有的共享资源
// 让系统中的某个文件夹共享或取消共享
//获得系统服务的运行状态
1.private void getServices()
2.{
3. ManagementObjectCollection queryCollection;
4. string[] lvData = new string[4];
5.
6. try
7. {
8. queryCollection = getServiceCollection("SELECT * FROM Win32_Service");
9. foreach ( ManagementObject mo in queryCollection)
10. {
11. //create child node for operating system
12. lvData[0] = mo["Name"].ToString();
13. lvData[1] = mo["StartMode"].ToString();
14. if (mo["Started"].Equals(true))
15. lvData[2] = "Started";
16. else
17. lvData[2] = "Stop";
18. lvData[3] = mo["StartName"].ToString();
19.
20. //create list item
21. ListViewItem lvItem = new ListViewItem(lvData,0);
22. listViewServices.Items.Add(lvItem);
23. }
24. }
25. catch (Exception e)
26. {
27. MessageBox.Show("Error: " + e);
28. }
29.}
// 修改IP,而实现不用重新启动
01.using System;
02.using System.Management;
03.using System.Threading;
04.
05.namespace WmiIpChanger
06.{
07. class IpChanger
08. {
09. [MTAThread]
10. static void Main(string[] args)
11. {
12. ReportIP();
13. // SwitchToDHCP();
14. SwitchToStatic();
15. Thread.Sleep( 5000 );
16. ReportIP();
17. Console.WriteLine( "end." );
18. }
19.
20. static void SwitchToDHCP()
21. {
22. ManagementBaseObject inPar = null;
23. ManagementBaseObject outPar = null;
24. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
25. ManagementObjectCollection moc = mc.GetInstances();
26. foreach( ManagementObject mo in moc )
27. {
28. if( ! (bool) mo["IPEnabled"] )
29. continue;
30.
31. inPar = mo.GetMethodParameters("EnableDHCP");
32. outPar = mo.InvokeMethod( "EnableDHCP", inPar, null );
33. break;
34. }
35. }
36.
37. static void SwitchToStatic()
38. {
39. ManagementBaseObject inPar = null;
40. ManagementBaseObject outPar = null;
41. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
42. ManagementObjectCollection moc = mc.GetInstances();
43. foreach( ManagementObject mo in moc )
44. {
45. if( ! (bool) mo[ "IPEnabled" ] )
46. continue;
47.
48. inPar = mo.GetMethodParameters( "EnableStatic" );
49. inPar["IPAddress"] = new string[] { "192.168.1.1" };
50. inPar["SubnetMask"] = new string[] { "255.255.255.0" };
51. outPar = mo.InvokeMethod( "EnableStatic", inPar, null );
52. break;
53. }
54. }
55.
56. static void ReportIP()
57. {
58. Console.WriteLine( "****** Current IP addresses:" );
59. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
60. ManagementObjectCollection moc = mc.GetInstances();
61. foreach( ManagementObject mo in moc )
62. {
63. if( ! (bool) mo[ "IPEnabled" ] )
64. continue;
65.
66. Console.WriteLine( "{0}\n SVC: '{1}' MAC: [{2}]", (string) mo["Caption"],
67. (string) mo["ServiceName"], (string) mo["MACAddress"] );
68.
69. string[] addresses = (string[]) mo[ "IPAddress" ];
70. string[] subnets = (string[]) mo[ "IPSubnet" ];
71.
72. Console.WriteLine( " Addresses :" );
73. foreach(string sad in addresses)
74. Console.WriteLine( "\t'{0}'", sad );
75.
76. Console.WriteLine( " Subnets :" );
77. foreach(string sub in subnets )
78. Console.WriteLine( "\t'{0}'", sub );
79. }
80. }
81. }
82.}