|
?.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+"\"");
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(" auseService",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???????????????????????????????????
????????????????
????????
??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??????????????????????????????????????????????????????????????????????
|
|