在.net中提供了一些類來顯示和控制Windows系統(tǒng)上的服務(wù),并可以實(shí)現(xiàn)對(duì)遠(yuǎn)程計(jì)算機(jī)服務(wù)服務(wù)的訪問,如System.ServiceProcess命名空間下面的ServiceController 類,System.Management下面的一些WMI操作的類。雖然用ServiceController可以很方便的實(shí)現(xiàn)對(duì)服務(wù)的控制,而且很直觀、簡(jiǎn)潔和容易理解。但是我認(rèn)為他的功能同通過WMI來操作服務(wù)相比,那可能就有些單一了,并且對(duì)多個(gè)服務(wù)的操作可能就比較麻煩,也無法列出系統(tǒng)中的所有服務(wù)的具體數(shù)據(jù)。這里要講的就是如何使用System.Management組件來操作遠(yuǎn)程和本地計(jì)算機(jī)上的服務(wù)。
WMI作為Windows 2000操作系統(tǒng)的一部分提供了可伸縮的,可擴(kuò)展的管理架構(gòu).公共信息模型(CIM)是由分布式管理任務(wù)標(biāo)準(zhǔn)協(xié)會(huì)(DMTF)設(shè)計(jì)的一種可擴(kuò)展的、面向?qū)ο蟮募軜?gòu),用于管理系統(tǒng)、網(wǎng)絡(luò)、應(yīng)用程序、數(shù)據(jù)庫和設(shè)備。Windows管理規(guī)范也稱作CIM for Windows,提供了統(tǒng)一的訪問管理信息的方式。如果需要獲取詳細(xì)的WMI信息請(qǐng)讀者查閱MSDN。System.Management組件提供對(duì)大量管理信息和管理事件集合的訪問,這些信息和事件是與根據(jù) Windows 管理規(guī)范 (WMI) 結(jié)構(gòu)對(duì)系統(tǒng)、設(shè)備和應(yīng)用程序設(shè)置檢測(cè)點(diǎn)有關(guān)的。
但是上面并不是我們最關(guān)心的,下面才是我們需要談的話題。
毫無疑問,我們要引用System.Management.Dll程序集,并要使用System.Management命名空間下的類,如ManagementClass,ManagementObject等。下面用一個(gè)名為Win32ServiceManager的類把服務(wù)的一些相關(guān)操作包裝了一下,代碼如下:
using System;
using System.Management;
namespace ZZ.Wmi
{
public class Win32ServiceManager
{
private string strPath;
private ManagementClass managementClass;
public Win32ServiceManager():this(".",null,null)
{
}
public Win32ServiceManager(string host,string userName,string password)
{
this.strPath = "\\\\"+host+"\\root\\cimv2:Win32_Service";
this.managementClass = new ManagementClass(strPath);
if(userName!=null&&userName.Length>0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
this.managementClass.Scope = managementScope;
}
}
// 驗(yàn)證是否能連接到遠(yuǎn)程計(jì)算機(jī)
public static bool RemoteConnectValidate(string host,string userName,string password)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
try
{
managementScope.Connect();
}
catch
{
}
return managementScope.IsConnected;
}
// 獲取指定服務(wù)屬性的值
public object GetServiceValue(string serviceName,string propertyName)
{
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
return mo[propertyName];
}
// 獲取所連接的計(jì)算機(jī)的所有服務(wù)數(shù)據(jù)
public string [,] GetServiceList()
{
string [,] services = new string [this.managementClass.GetInstances().Count,4];
int i = 0;
foreach(ManagementObject mo in this.managementClass.GetInstances())
{
services[i,0] = (string)mo["Name"];
services[i,1] = (string)mo["DisplayName"];
services[i,2] = (string)mo["State"];
services[i,3] = (string)mo["StartMode"];
i++;
}
return services;
}
// 獲取所連接的計(jì)算機(jī)的指定服務(wù)數(shù)據(jù)
WMI作為Windows 2000操作系統(tǒng)的一部分提供了可伸縮的,可擴(kuò)展的管理架構(gòu).公共信息模型(CIM)是由分布式管理任務(wù)標(biāo)準(zhǔn)協(xié)會(huì)(DMTF)設(shè)計(jì)的一種可擴(kuò)展的、面向?qū)ο蟮募軜?gòu),用于管理系統(tǒng)、網(wǎng)絡(luò)、應(yīng)用程序、數(shù)據(jù)庫和設(shè)備。Windows管理規(guī)范也稱作CIM for Windows,提供了統(tǒng)一的訪問管理信息的方式。如果需要獲取詳細(xì)的WMI信息請(qǐng)讀者查閱MSDN。System.Management組件提供對(duì)大量管理信息和管理事件集合的訪問,這些信息和事件是與根據(jù) Windows 管理規(guī)范 (WMI) 結(jié)構(gòu)對(duì)系統(tǒng)、設(shè)備和應(yīng)用程序設(shè)置檢測(cè)點(diǎn)有關(guān)的。
但是上面并不是我們最關(guān)心的,下面才是我們需要談的話題。
毫無疑問,我們要引用System.Management.Dll程序集,并要使用System.Management命名空間下的類,如ManagementClass,ManagementObject等。下面用一個(gè)名為Win32ServiceManager的類把服務(wù)的一些相關(guān)操作包裝了一下,代碼如下:
using System;
using System.Management;
namespace ZZ.Wmi
{
public class Win32ServiceManager
{
private string strPath;
private ManagementClass managementClass;
public Win32ServiceManager():this(".",null,null)
{
}
public Win32ServiceManager(string host,string userName,string password)
{
this.strPath = "\\\\"+host+"\\root\\cimv2:Win32_Service";
this.managementClass = new ManagementClass(strPath);
if(userName!=null&&userName.Length>0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
this.managementClass.Scope = managementScope;
}
}
// 驗(yàn)證是否能連接到遠(yuǎn)程計(jì)算機(jī)
public static bool RemoteConnectValidate(string host,string userName,string password)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
try
{
managementScope.Connect();
}
catch
{
}
return managementScope.IsConnected;
}
// 獲取指定服務(wù)屬性的值
public object GetServiceValue(string serviceName,string propertyName)
{
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
return mo[propertyName];
}
// 獲取所連接的計(jì)算機(jī)的所有服務(wù)數(shù)據(jù)
public string [,] GetServiceList()
{
string [,] services = new string [this.managementClass.GetInstances().Count,4];
int i = 0;
foreach(ManagementObject mo in this.managementClass.GetInstances())
{
services[i,0] = (string)mo["Name"];
services[i,1] = (string)mo["DisplayName"];
services[i,2] = (string)mo["State"];
services[i,3] = (string)mo["StartMode"];
i++;
}
return services;
}
// 獲取所連接的計(jì)算機(jī)的指定服務(wù)數(shù)據(jù)