日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#ServiceController類剖析

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

創(chuàng)新互聯(lián)2013年開(kāi)創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元青州做網(wǎng)站,已為上家服務(wù),為青州各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

在.net中提供了一些類來(lái)顯示和控制Windows系統(tǒng)上的服務(wù),并可以實(shí)現(xiàn)對(duì)遠(yuǎn)程計(jì)算機(jī)服務(wù)服務(wù)的訪問(wèn),如System.ServiceProcess命名空間下面的C# ServiceController類。

C# ServiceController類可以很方便的實(shí)現(xiàn)對(duì)服務(wù)的控制,而且很直觀、簡(jiǎn)潔和容易理解。但是我認(rèn)為他的功能同通過(guò)WMI來(lái)操作服務(wù)相比,那可能就有些單一了,并且對(duì)多個(gè)服務(wù)的操作可能就比較麻煩,也無(wú)法列出系統(tǒng)中的所有服務(wù)的具體數(shù)據(jù)。這里要講的就是如何使用System.Management組件來(lái)操作遠(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ù)庫(kù)和設(shè)備。Windows管理規(guī)范也稱作CIM for Windows,提供了統(tǒng)一的訪問(wèn)管理信息的方式。如果需要獲取詳細(xì)的WMI信息請(qǐng)讀者查閱MSDN。System.Management組件提供對(duì)大量管理信息和管理事件集合的訪問(wèn),這些信息和事件是與根據(jù) Windows 管理規(guī)范 (WMI) 結(jié)構(gòu)對(duì)系統(tǒng)、設(shè)備和應(yīng)用程序設(shè)置檢測(cè)點(diǎn)有關(guān)的。

但是上面并不是我們最關(guān)心的,下面才是我們需要談的話題。

毫無(wú)疑問(wèn),我們要引用System.Management.Dll程序集,并要使用System.Management命名空間下的類,如 ManagementClass,ManagementObject等。下面用一個(gè)名為Win32ServiceManager的類把服務(wù)的一些相關(guān)操作包裝了一下,代碼如下:

 
 
 
  1. usingSystem;  
  2. usingSystem.Management;  
  3. namespaceZZ.Wmi  
  4. {  
  5. publicclassWin32ServiceManager  
  6. {  
  7. privatestringstrPath;  
  8. privateManagementClassmanagementClass;  
  9. publicWin32ServiceManager():this(".",null,null)  
  10. {  
  11. }  
  12. publicWin32ServiceManager(stringhost,stringuserName,stringpassword)  
  13. {  
  14. this.strPath="\\\\"+host+"\\root\\cimv2:Win32_Service";  
  15. this.managementClass=newManagementClass(strPath);  
  16. if(userName!=null&&userName.Length>0)  
  17. {  
  18. ConnectionOptionsconnectionOptions=newConnectionOptions();  
  19. connectionOptions.Username=userName;  
  20. connectionOptions.Password=password;  
  21. ManagementScopemanagementScope=newManagementScope
    ("\\\\"+host+"\\root\\cimv2",connectionOptions);  
  22. this.managementClass.Scope=managementScope;  
  23. }  
  24. }  
  25. //驗(yàn)證是否能連接到遠(yuǎn)程計(jì)算機(jī)  
  26. publicstaticboolRemoteConnectValidate(stringhost,stringuserName,stringpassword)  
  27. {  
  28. ConnectionOptionsconnectionOptions=newConnectionOptions();  
  29. connectionOptions.Username=userName;  
  30. connectionOptions.Password=password;  
  31. ManagementScopemanagementScope=newManagementScope
    ("\\\\"+host+"\\root\\cimv2",connectionOptions);  
  32. try  
  33. {  
  34. managementScope.Connect();  
  35. }  
  36. catch  
  37. {  
  38. }  
  39. returnmanagementScope.IsConnected;  
  40. }  
  41. //獲取指定服務(wù)屬性的值  
  42. publicobjectGetServiceValue(stringserviceName,stringpropertyName)  
  43. {  
  44. ManagementObjectmo=this.managementClass.CreateInstance();  
  45. mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");  
  46. returnmo[propertyName];  
  47. }  
  48. //獲取所連接的計(jì)算機(jī)的所有服務(wù)數(shù)據(jù)  
  49. publicstring[,]GetServiceList()  
  50. {  
  51. string[,]services=newstring[this.managementClass.GetInstances().Count,4];  
  52. inti=0;  
  53. foreach(ManagementObjectmointhis.managementClass.GetInstances())  
  54. {  
  55. services[i,0]=(string)mo["Name"];  
  56. services[i,1]=(string)mo["DisplayName"];  
  57. services[i,2]=(string)mo["State"];  
  58. services[i,3]=(string)mo["StartMode"];  
  59. i++;  
  60. }  
  61. returnservices;  
  62. }  
  63. //獲取所連接的計(jì)算機(jī)的指定服務(wù)數(shù)據(jù)  
  64. publicstring[,]GetServiceList(stringserverName)  
  65. {  
  66. returnGetServiceList(newstring[]{serverName});  
  67. }  
  68. //獲取所連接的計(jì)算機(jī)的的指定服務(wù)數(shù)據(jù)  
  69. publicstring[,]GetServiceList(string[]serverNames)  
  70. {  
  71. string[,]services=newstring[serverNames.Length,4];  
  72. ManagementObjectmo=this.managementClass.CreateInstance();  
  73. for(inti=0;i  
  74. {  
  75. mo.Path=newManagementPath(this.strPath+".Name=\""+serverNames[i]+"\"");  
  76. services[i,0]=(string)mo["Name"];  
  77. services[i,1]=(string)mo["DisplayName"];  
  78. services[i,2]=(string)mo["State"];  
  79. services[i,3]=(string)mo["StartMode"];  
  80. }  
  81. returnservices;  
  82. }  
  83. //停止指定的服務(wù)  
  84. publicstringStartService(stringserviceName)  
  85. {  
  86. stringstrRst=null;  
  87. ManagementObjectmo=this.managementClass.CreateInstance();  
  88. mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");  
  89. try  
  90. {  
  91. if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]  
  92. mo.InvokeMethod("StartService",null);  
  93. }  
  94. catch(ManagementExceptione)  
  95. {  
  96. strRst=e.Message;  
  97. }  
  98. returnstrRst;  
  99. }  
  100. //暫停指定的服務(wù)  
  101. publicstringPauseService(stringserviceName)  
  102. {  
  103. stringstrRst=null;  
  104. ManagementObjectmo=this.managementClass.CreateInstance();  
  105. mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");  
  106. try  
  107. {  
  108. //判斷是否可以暫停  
  109. if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")  
  110. mo.InvokeMethod("PauseService",null);  
  111. }  
  112. catch(ManagementExceptione)  
  113. {  
  114. strRst=e.Message;  
  115. }  
  116. returnstrRst;  
  117. }  
  118. //恢復(fù)指定的服務(wù)  
  119. publicstringResumeService(stringserviceName)  
  120. {  
  121. stringstrRst=null;  
  122. ManagementObjectmo=this.managementClass.CreateInstance();  
  123. mo.Path=newManagementPath(this.strPath+".Name=\""+serviceName+"\"");  
  124. try  
  125. {  
  126. //判斷是否可以恢復(fù)  
  127. if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")  
  128. mo.InvokeMethod("ResumeService",null);  
  129. }  
  130. catch(ManagementExceptione)  
  131. {  
  132. strRst=e.Message;  
  133. }  
  134. returnstrRst;  

以上介紹C# ServiceController類。


網(wǎng)站名稱:C#ServiceController類剖析
文章鏈接:http://www.5511xx.com/article/djsjegc.html