麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

運用C#控制遠程計算機服務的實現方法

2023-05-12 12:29:37
字體:
來源:轉載
供稿:網友

在.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];

}

// 獲取所連接的計算機的所有服務數據

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;

}

// 獲取所連接的計算機的指定服務數據

public string [,] GetServiceList(string serverName)

{

return GetServiceList(new string []{serverName});

}

// 獲取所連接的計算機的的指定服務數據

public string [,] GetServiceList(string [] serverNames)

{

string [,] services = new string [serverNames.Length,4];

ManagementObject mo = this.managementClass.CreateInstance();

for(int i = 0;i<serverNames.Length;i++)

{

mo.Path = new ManagementPath(

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"];

} return services;

}

// 停止指定的服務

public string StartService(string serviceName)

{ string strRst = null;

ManagementObject mo = this.managementClass.CreateInstance();

mo.Path = new ManagementPath(

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

try

{

if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]

mo.InvokeMethod("StartService",null);

}

catch(ManagementException e)

{ strRst =e.Message;

}

return strRst;

}

// 暫停指定的服務

public string PauseService(string serviceName)

{

string strRst = null;

ManagementObject mo = this.managementClass.CreateInstance();

mo.Path = new ManagementPath(

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

try

{

//判斷是否可以暫停

if((bool)mo["acceptPause"]&&(string)mo["State"]=="Running")

mo.InvokeMethod("PauseService",null);

}

catch(ManagementException e)

{

strRst =e.Message;

}

return strRst;

}

// 恢復指定的服務

public string ResumeService(string serviceName)

{

string strRst = null;

ManagementObject mo = this.managementClass.CreateInstance();

mo.Path = new ManagementPath(this.strPath+".Name=/""+serviceName+"/"");

try

{

//判斷是否可以恢復

if((bool)mo["acceptPause"]&&(string)mo["State"]=="Paused")

mo.InvokeMethod("ResumeService",null);

}

catch(ManagementException e)

{

strRst =e.Message;

}

return strRst;

}

// 停止指定的服務

public string StopService(string serviceName)

{ string strRst = null;

ManagementObject mo = this.managementClass.CreateInstance();

mo.Path = new ManagementPath(

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

try

{

//判斷是否可以停止

if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"

mo.InvokeMethod("StopService",null);

}

catch(ManagementException e)

{

strRst =e.Message;

}

return strRst;

}

}

}

在Win32ServiceManager中通過RemoteConnectValidate靜態方法來測試連接成功與否;另外提供了GetServiceValue方法和GetServiceList方法以及它的重載來獲取服務信息;后面的四個方法就是對服務的狀態控制了。

下面建立一個簡單的窗口來使用它。

大致的界面如下:

顯示窗體_武林網VEVB(http://www.companysz.com)

通過vs.net 2003可以很快做出上面的窗體,下面列出了一些增加的代碼:

 


 

using ZZ.Wmi;

namespace ZZForm

{

public class Form1 : System.Windows.Forms.Form

{

//……

private Win32ServiceManager serviceManager;

public Form1()

{

InitializeComponent();

this.serviceManager = null;

}

//……

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

//修改服務狀態

private void buttonChangeState_Click(object sender, System.EventArgs e)

{

switch(((Button)sender).Text)

{

case "啟動":

string startRst = this.serviceManager.StartService(

this.listViewService.SelectedItems[0].SubItems[0].Text);

if(startRst==null)

MessageBox.Show("操作成功,請點擊獲取刷新按鈕刷新結果!");

else

MessageBox.Show(startRst);

break;

case "暫停":

string startPause = this.serviceManager.PauseService(

this.listViewService.SelectedItems[0].SubItems[0].Text);

if(startPause==null)

MessageBox.Show("操作成功,請點擊獲取刷新按鈕刷新結果!");

else

MessageBox.Show(startPause);

break;

case "繼續":

string startResume = this.serviceManager.ResumeService(

this.listViewService.SelectedItems[0].SubItems[0].Text);

if(startResume==null)

MessageBox.Show("操作成功,請點擊獲取刷新按鈕刷新結果!");

else

MessageBox.Show(startResume);

break;

case "停止":

string startStop = this.serviceManager.StopService(

this.listViewService.SelectedItems[0].SubItems[0].Text);

if(startStop==null)

MessageBox.Show("操作成功,請點擊獲取刷新按鈕刷新結果!");

else

MessageBox.Show(startStop);

break;

}

}

//獲取和刷新數據

private void buttonLoadRefresh_Click(object sender, System.EventArgs e)

{

if(this.textBoxHost.Text.Trim().Length>0)

{

if(this.textBoxHost.Text.Trim()==".")

{

this.serviceManager = new Win32ServiceManager();

}

else

{

if(Win32ServiceManager.RemoteConnectValidate(this.textBoxHost.Text.Trim(),

this.textBoxName.Text.Trim(),this.textBoxPassword.Text.Trim()))

{

this.serviceManager = new Win32ServiceManager(this.textBoxHost.Text.Trim(),

this.textBoxName.Text.Trim(),this.textBoxPassword.Text.Trim());

}

else

{

MessageBox.Show("連接到遠程計算機驗證錯誤.");

return;

}

}

string [,] services = serviceManager.GetServiceList();

this.listViewService.BeginUpdate();

this.listViewService.Items.Clear();

for(int i=0;i<services.GetLength(0);i++)

{

ListViewItem item = new ListViewItem(new string[]{

services[i,0],services[i,1],services[i,2],services[i,3]});

this.listViewService.Items.Add(item);

}

this.listViewService.EndUpdate();

}

else

MessageBox.Show("請輸入計算機名或IP地址");

}

}

}

說明,其實一個服務的屬性和方法除了上面這幾個還有很多,我們可以通過實例化ManagementClass類,使用它的Properties屬性和Methods屬性列出所有的屬性和方法。上面的Win32ServiceManager中生成的每個服務實例都是ManagementObejct類型的,其實還有一種強類型的類,可以通過編程和工具來生成。

總結,通過引用System.Management命名空間,上面簡單的實現了通過訪問/root/cimv2:Win32_Service名稱空間對服務進行顯示和操作。此外,我們還可以通過訪問其他名稱空間來訪問計算機的一些硬件信息,軟件信息以及網絡等,有興趣的讀者可以研究一下。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91亚洲精品一区二区福利 | 欧美18xxxx| 92看片淫黄大片欧美看国产片 | 黄色av.com | 精品国产乱码久久久久久久久 | 国产日产精品一区二区三区四区 | 久久精热 | 久久精品亚洲一区 | 青青国产在线视频 | 日本精品免费观看 | 一级看片免费视频 | 成人免费在线播放 | 国产毛片aaa一区二区三区视频 | 亚州视频在线 | 成人精品久久久 | 欧美国产一区二区三区 | 激情视频导航 | 黄色网欧美 | 久久久久久久久成人 | 久草在线新时代视觉 | 久久久久久艹 | 国产亚洲美女精品久久久2020 | 国产精品免费久久久久久 | 久久久毛片视频 | 国产无遮挡一级毛片 | 中文日产幕无线码6区免费版 | 久草在线网址 | 宅男噜噜噜66一区二区 | 九九热国产在线 | 国产精品一区网站 | 国产精品久久久久久久午夜片 | 亚洲精品一区中文字幕 | 成人福利在线免费观看 | 97中文| 神秘电影91 | 一级黄色在线免费观看 | 亚洲第一成人av | 精品一区二区三区免费 | 国产视频在线观看免费 | 国产精品久久久久久久久久iiiii | 午夜视频在线观看免费视频 |