C# 获取远程机器上的指定进程路径
时间: 2023-11-25 16:06:33 浏览: 159
要获取远程计算机上正在运行的进程的路径,可以使用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())
{
string processPath = (string)managementObject["ExecutablePath"];
// do something with processPath
}
}
```
请将RemoteMachineName,ProcessName,RemoteMachineUsername和RemoteMachinePassword替换为实际的值。请注意,需要在远程计算机上启用WMI服务,并且RemoteMachineUsername必须具有足够的权限才能执行此操作。
阅读全文