- 编程
C#实现远程终端控制
- 2023-8-19 20:30:41 @
不多说废话(没话说)
核OJ上手敲的,敲完才发现没缩进
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")
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"])
mo.InvokeMethod("StopService",null);
}
catch(ManagementException e)
{
strRst =e.Message;
}
return strRst;
}
}
}
然后写了个窗体……
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地址");
}
}
}
就这样吧(超,)