1、  主要使用到的组件有System.Management.dll
  2、  主要类为 :ManagementScope
  连接远程服务器示例代码:
1 private const string PROPERTY_CAPACITY = "Capacity";
2         private const string PROPERTY_AVAILABLE_BYTES = "AvailableBytes";
3         private const string PROPERTY_PROCESS_ID = "ProcessId";
4         private const string PROPERTY_PROCESS_NAME = "Name";
5         private const string PROPERTY_WORKING_SET_SIZE = "WorkingSetSize";
6
7
8 ConnectionOptions Conn = new ConnectionOptions();
9                 //设定用于WMI连接操作的用户名
10                 Conn.Username = "用户名";
11                 //设定用户的口令
12                 Conn.Password = "密码";
13              ManagementScope   Ms = new         ManagementScope("\\ip\root\cimv2", Conn);
14                 Ms.Connect();
  具体查询举例:
1 var query1 = new SelectQuery("SELECT * FROM Win32_PhysicalMemory");
2  var query2 = new SelectQuery("SELECT * FROM Win32_PerfRawData_PerfOS_Memory");
3   var query3 = new SelectQuery("SELECT * FROM Win32_Processor");
4  var query4 = new SelectQuery("SELECT * FROM Win32_Process");
5
6  var searcher1 = new ManagementObjectSearcher(Ms, query1);
7  var searcher2 = new ManagementObjectSearcher(Ms, query2);
8 var searcher3 = new ManagementObjectSearcher(Ms, query3);
9 var searcher4 = new ManagementObjectSearcher(Ms, query4);
  查询内存和CPU:
1 var capacity = 0.0;
2    var free = 0.0;
3   var cpu = 0.0;
4  // 2048
5    foreach (var o in searcher1.Get())
6capacity += (double)Convert.ToUInt64(o[PROPERTY_CAPACITY]) / (double)(1024 * 1024);
7                         foreach (var o in searcher2.Get())
8                             free += (double)Convert.ToUInt64(o[PROPERTY_AVAILABLE_BYTES]) / (double)(1024 * 1024);
9                         foreach (var o in searcher3.Get())
10                         {
11                             if (o["LoadPercentage"] != null)
12                             {
13                                 cpu = long.Parse(o["LoadPercentage"].ToString());
14                             }
15                             else {
16                                 cpu = 0;
17                             }
18                         }
  举例远程重启某个进程(部分示例代码):
1 ConnectionOptions Conn = new ConnectionOptions();
2             Conn.Username = userName; //设定用户的口令
3             Conn.Password = pwd;
4             Ms = new ManagementScope("\\" + ip + "\root\cimv2", Conn);
5             Ms.Connect();
6  ManagementClass processClass = new ManagementClass(Ms, new ManagementPath("Win32_Process"), null);
7             //获得用来提供参数的对象
8             ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
9             //设定命令行参数
10                 inParams["CommandLine"] = path;// @"需要启动的进程路径";
11                 //执行程序
12             ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
  Other、可以用程序操作批处理文件(.bat)来查询进程或者是关闭进程:
  例如查询所有进程 :tasklist /S ip /U 用户名 /P 密码
  关闭某个进程:
  taskkill  /S ip /U用户名/P 密码  /IM 需要关闭的进程名