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];
}
//获取所连接的计算机的所有服务数据
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;
}
//获取所连接的计算机的指定服务数据
publicstring[,]GetServiceList(stringserverName)
{
returnGetServiceList(newstring[]{serverName});
}
//获取所连接的计算机的的指定服务数据
publicstring[,]GetServiceList(string[]serverNames)
{
string[,]services=newstring[serverNames.Length,4];
ManagementObjectmo=this.managementClass.CreateInstance();
for(inti=0;i<serverNames.Length;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;
}
//停止指定的服务
publicstringStopService(stringserviceName)
{
stringstrRst=null;
ManagementObjectmo=this.managementClass.CreateInstance();
mo.Path=newManagementPath(this.strPath+".Name=""+serviceName+""");
try
{
//判断是否可以停止
if((bool)mo["AcceptStop"])//(string)mo["State"]=="Running"
mo.InvokeMethod("StopService",null);
}
catch(ManagementExceptione)
{
strRst=e.Message;
}
returnstrRst;
}
}
}
publicclassForm1:System.Windows.Forms.Form
{
//……
privateWin32ServiceManagerserviceManager;
publicForm1()
{
InitializeComponent();
this.serviceManager=null;
}
//……
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
//修改服务状态
privatevoidbuttonChangeState_Click(objectsender,System.EventArgse)
{
switch(((Button)sender).Text)
{
case"启动":
stringstartRst=this.serviceManager.StartService(this.listViewService.SelectedItems[0].SubItems[0].Text);
if(startRst==null)
MessageBox.Show("操作成功,请点击获取刷新按钮刷新结果!");
else
MessageBox.Show(startRst);
break;
case"暂停":
stringstartPause=this.serviceManager.PauseService(this.listViewService.SelectedItems[0].SubItems[0].Text);
if(startPause==null)
MessageBox.Show("操作成功,请点击获取刷新按钮刷新结果!");
else
MessageBox.Show(startPause);
break;
case"继续":
stringstartResume=this.serviceManager.ResumeService(this.listViewService.SelectedItems[0].SubItems[0].Text);
if(startResume==null)
MessageBox.Show("操作成功,请点击获取刷新按钮刷新结果!");
else
MessageBox.Show(startResume);
break;
case"停止":
stringstartStop=this.serviceManager.StopService(this.listViewService.SelectedItems[0].SubItems[0].Text);
if(startStop==null)
MessageBox.Show("操作成功,请点击获取刷新按钮刷新结果!");
else
MessageBox.Show(startStop);
break;
}
}
//获取和刷新数据
privatevoidbuttonLoadRefresh_Click(objectsender,System.EventArgse)
{
if(this.textBoxHost.Text.Trim().Length>0)
{
if(this.textBoxHost.Text.Trim()==".")
{
this.serviceManager=newWin32ServiceManager();
}
else
{
if(Win32ServiceManager.RemoteConnectValidate(this.textBoxHost.Text.Trim(),this.textBoxName.Text.Trim(),this.textBoxPassword.Text.Trim()))
{
this.serviceManager=newWin32ServiceManager(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(inti=0;i<services.GetLength(0);i++)
{
ListViewItemitem=newListViewItem(newstring[]{services[i,0],services[i,1],services[i,2],services[i,3]});
this.listViewService.Items.Add(item);
}
this.listViewService.EndUpdate();
}
else
MessageBox.Show("请输入计算机名或IP地址");
}
}
}