c# 远程关闭exe
时间: 2023-08-02 10:25:48 浏览: 167
c#实现远程关机
要关闭远程计算机上的正在运行的进程,可以使用C#中的WMI(Windows Management Instrumentation)服务。以下是一个示例代码:
```csharp
using System.Management;
string remoteMachineName = "RemoteMachineName";
string processName = "ProcessName";
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = "RemoteMachineUsername";
connectionOptions.Password = "RemoteMachinePassword";
connectionOptions.EnablePrivileges = true;
ManagementScope managementScope = new ManagementScope($@"\\{remoteMachineName}\root\cimv2", connectionOptions);
managementScope.Connect();
SelectQuery selectQuery = new SelectQuery($"select * from Win32_Process where Name='{processName}'");
using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(managementScope, selectQuery))
{
foreach (ManagementObject managementObject in managementObjectSearcher.Get())
{
managementObject.InvokeMethod("Terminate", null);
}
}
```
请将RemoteMachineName,ProcessName,RemoteMachineUsername和RemoteMachinePassword替换为实际的值。请注意,需要在远程计算机上启用WMI服务,并且RemoteMachineUsername必须具有足够的权限才能执行此操作。
阅读全文