MrP*_*ram 11 c# process remote-access kill-process
我正试图杀死远程机器上的进程.但我得到错误.我做错了什么,我该怎么做才能做到这一点?
我的代码:
var iu = new ImpersonateUser();
try
{
iu.Impersonate(Domain, _userName, _pass);
foreach (var process in Process.GetProcessesByName("notepad", "RemoteMachine"))
{
string processPath = pathToExe; //Is set as constant (and is correct)
process.Kill();
Thread.Sleep(3000);
Process.Start(processPath);
}
}
catch (Exception ex)
{
lblStatus.Text = ex.ToString();
}
finally
{
iu.Undo();
}
Run Code Online (Sandbox Code Playgroud)
只是为了澄清ImpersonateUser,它让我以正确的用户权限登录到远程机器.所以问题不存在.当我调试并检查过程对象时,在这种情况下我找到了记事本的正确进程ID.所以连接工作正常.但是当我试图杀死进程时,我得到了这个错误:
System.NotSupportedException: Feature is not supported for remote machines. at System.Diagnostics.Process.EnsureState
Run Code Online (Sandbox Code Playgroud)
Dav*_*ell 22
本System.Diagnostics.Process类不能终止远程进程.您可以使用System.Management命名空间(确保设置引用)来使用WMI.
一个简单的例子如下.
var processName = "iexplore.exe";
var connectoptions = new ConnectionOptions();
connectoptions.Username = @"YourDomainName\UserName";
connectoptions.Password = "User Password";
string ipAddress = "192.168.206.53";
ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2", connectoptions);
// WMI query
var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
using (var searcher = new ManagementObjectSearcher(scope, query))
{
foreach (ManagementObject process in searcher.Get()) // this is the fixed line
{
process.InvokeMethod("Terminate", null);
}
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10439 次 |
| 最近记录: |