使用C#控制遠端電腦的服務

來源:互聯網
上載者:User

·                                 使用C#控制遠端電腦的服務

·                                 http://developer.51cto.com  2006-01-25 16:47  TrackBack  tb.blog.csdn.net/TrackBack  我要評論(2)

在.net中提供了一些類來顯示和控制Windows系統上的服務,並可以實現對遠端電腦服務服務的訪問。本文講解的是是如何使用System.Management組件來操作遠程和本機電腦上的服務。

在.net中提供了一些類來顯示和控制Windows系統上的服務,並可以實現對遠端電腦服務服務的訪問,如System.ServiceProcess命名空間下面的ServiceController 類,System.Management下面的一些WMI操作的類。雖然用ServiceController可以很方便的實現對服務的控制,而且很直觀、簡潔和容易理解。但是我認為他的功能同通過WMI來操作服務相比,那可能就有些單一了,並且對多個服務的操作可能就比較麻煩,也無法列出系統中的所有服務的具體資料。這裡要講的就是如何使用System.Management組件來操作遠程和本機電腦上的服務。

WMI作為Windows 2000作業系統的一部分提供了可伸縮的,可擴充的管理架構.公用資訊模型(CIM)是由分布式管理工作標準協會(DMTF)設計的一種可擴充的、物件導向的架構,用於管理系統、網路、應用程式、資料庫和裝置。Windows管理規範也稱作CIM for Windows,提供了統一的訪問管理資訊的方式。如果需要擷取詳細的WMI資訊請讀者查閱MSDN。System.Management組件提供對大量管理資訊和管理事件集合的訪問,這些資訊和事件是與根據 Windows 管理規範 (WMI) 結構對系統、裝置和應用程式設定檢測點有關的。

但是上面並不是我們最關心的,下面才是我們需要談的話題。毫無疑問,我們要引用System.Management.Dll程式集,並要使用System.Management命名空間下的類,如ManagementClass,ManagementObject等。下面用一個名為Win32ServiceManager的類把服務的一些相關操作封裝了一下,代碼如下:

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;

 

}

}

 

// 驗證是否能串連到遠端電腦

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;

}

 

// 擷取指定服務屬性的值

public object GetServiceValue(string serviceName,string propertyName)

{

ManagementObject mo = this.managementClass.CreateInstance();

mo.Path = new ManagementPath(

this.strPath+".Name=/""+serviceName+"/"");

return mo[propertyName];

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.