新聞中心
C#語言有很多值得學習的地方,這里我們主要介紹C# ServiceController類,包括介紹WMI作為Windows 2000操作系統(tǒng)的一部分等方面。

創(chuàng)新互聯(lián)2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都網(wǎng)站設計、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元青州做網(wǎng)站,已為上家服務,為青州各地企業(yè)和個人服務,聯(lián)系電話:028-86922220
在.net中提供了一些類來顯示和控制Windows系統(tǒng)上的服務,并可以實現(xiàn)對遠程計算機服務服務的訪問,如System.ServiceProcess命名空間下面的C# ServiceController類。
C# ServiceController類可以很方便的實現(xiàn)對服務的控制,而且很直觀、簡潔和容易理解。但是我認為他的功能同通過WMI來操作服務相比,那可能就有些單一了,并且對多個服務的操作可能就比較麻煩,也無法列出系統(tǒng)中的所有服務的具體數(shù)據(jù)。這里要講的就是如何使用System.Management組件來操作遠程和本地計算機上的服務。
WMI作為Windows 2000操作系統(tǒng)的一部分提供了可伸縮的,可擴展的管理架構.公共信息模型(CIM)是由分布式管理任務標準協(xié)會(DMTF)設計的一種可擴展的、面向?qū)ο蟮募軜?,用于管理系統(tǒng)、網(wǎng)絡、應用程序、數(shù)據(jù)庫和設備。Windows管理規(guī)范也稱作CIM for Windows,提供了統(tǒng)一的訪問管理信息的方式。如果需要獲取詳細的WMI信息請讀者查閱MSDN。System.Management組件提供對大量管理信息和管理事件集合的訪問,這些信息和事件是與根據(jù) Windows 管理規(guī)范 (WMI) 結構對系統(tǒng)、設備和應用程序設置檢測點有關的。
但是上面并不是我們最關心的,下面才是我們需要談的話題。
毫無疑問,我們要引用System.Management.Dll程序集,并要使用System.Management命名空間下的類,如 ManagementClass,ManagementObject等。下面用一個名為Win32ServiceManager的類把服務的一些相關操作包裝了一下,代碼如下:
- usingSystem;
- usingSystem.Management;
- namespaceZZ.Wmi
- {
- publicclassWin32ServiceManager
- {
- privatestringstrPath;
- privateManagementClassmanagementClass;
- publicWin32ServiceManager():this(".",null,null)
- {
- }
- publicWin32ServiceManager(stringhost,stringuserName,stringpassword)
- {
- this.strPath="\\\\"+host+"\\root\\cimv2:Win32_Service";
- this.managementClass=newManagementClass(strPath);
- if(userName!=null&&userName.Length>0)
- {
- ConnectionOptionsconnectionOptions=newConnectionOptions();
- connectionOptions.Username=userName;
- connectionOptions.Password=password;
- ManagementScopemanagementScope=newManagementScope
("\\\\"+host+"\\root\\cimv2",connectionOptions);- this.managementClass.Scope=managementScope;
- }
- }
- //驗證是否能連接到遠程計算機
- publicstaticboolRemoteConnectValidate(stringhost,stringuserName,stringpassword)
- {
- ConnectionOptionsconnectionOptions=newConnectionOptions();
- connectionOptions.Username=userName;
- connectionOptions.Password=password;
- ManagementScopemanagementScope=newManagementScope
("\\\\"+host+"\\root\\cimv2",connectionOptions);- try
- {
- managementScope.Connect();
- }
- catch
- {
- }
- returnmanagementScope.IsConnected;
- }
- //獲取指定服務屬性的值
- publicobjectGetServiceValue(stringserviceName,stringpropertyName)
- {
- ManagementObjectmo=this.managementClass.CreateInstance();
- mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");
- returnmo[propertyName];
- }
- //獲取所連接的計算機的所有服務數(shù)據(jù)
- publicstring[,]GetServiceList()
- {
- string[,]services=newstring[this.managementClass.GetInstances().Count,4];
- inti=0;
- foreach(ManagementObjectmointhis.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++;
- }
- returnservices;
- }
- //獲取所連接的計算機的指定服務數(shù)據(jù)
- publicstring[,]GetServiceList(stringserverName)
- {
- returnGetServiceList(newstring[]{serverName});
- }
- //獲取所連接的計算機的的指定服務數(shù)據(jù)
- publicstring[,]GetServiceList(string[]serverNames)
- {
- string[,]services=newstring[serverNames.Length,4];
- ManagementObjectmo=this.managementClass.CreateInstance();
- for(inti=0;i
- {
- mo.Path=newManagementPath(this.strPath+".Name=\""+serverNames[i]+"\"");
- services[i,0]=(string)mo["Name"];
- services[i,1]=(string)mo["DisplayName"];
- services[i,2]=(string)mo["State"];
- services[i,3]=(string)mo["StartMode"];
- }
- returnservices;
- }
- //停止指定的服務
- publicstringStartService(stringserviceName)
- {
- stringstrRst=null;
- ManagementObjectmo=this.managementClass.CreateInstance();
- mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");
- try
- {
- if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]
- mo.InvokeMethod("StartService",null);
- }
- catch(ManagementExceptione)
- {
- strRst=e.Message;
- }
- returnstrRst;
- }
- //暫停指定的服務
- publicstringPauseService(stringserviceName)
- {
- stringstrRst=null;
- ManagementObjectmo=this.managementClass.CreateInstance();
- mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");
- try
- {
- //判斷是否可以暫停
- if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")
- mo.InvokeMethod("PauseService",null);
- }
- catch(ManagementExceptione)
- {
- strRst=e.Message;
- }
- returnstrRst;
- }
- //恢復指定的服務
- publicstringResumeService(stringserviceName)
- {
- stringstrRst=null;
- ManagementObjectmo=this.managementClass.CreateInstance();
- mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");
- try
- {
- //判斷是否可以恢復
- if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")
- mo.InvokeMethod("ResumeService",null);
- }
- catch(ManagementExceptione)
- {
- strRst=e.Message;
- }
- returnstrRst;
- }
以上介紹C# ServiceController類。
網(wǎng)頁題目:C#ServiceController類剖析
當前網(wǎng)址:http://m.fisionsoft.com.cn/article/djsjegc.html


咨詢
建站咨詢
