Csharp/C#教程:杀死远程机器上的进程分享


杀死远程机器上的进程

我正试图杀死远程机器上的进程。 但我得到错误。 我做错了什么,我该怎么做才能做到这一点?

我的代码:

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(); } 

只是为了澄清ImpersonateUser,它让我以正确的用户权限登录到远程机器。 所以问题不在那里。 当我调试并检查过程对象时,在这种情况下我找到了记事本的正确进程ID。 所以连接工作正常。 但是当我尝试杀死进程时,我收到此错误:

 System.NotSupportedException: Feature is not supported for remote machines. at System.Diagnostics.Process.EnsureState 

System.Diagnostics.Process类无法终止远程进程。 您可以使用System.Management命名空间(确保设置引用),以使用WMI。

一个简单的例子如下。

上述就是C#学习教程:杀死远程机器上的进程分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 var processName = "iexplore.exe"; var connectoptions = new ConnectionOptions(); connectoptions.Username = @"YourDomainNameUserName"; connectoptions.Password = "User Password"; string ipAddress = "192.168.206.53"; ManagementScope scope = new ManagementScope(@"\" + ipAddress + @"rootcimv2", 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(); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/989190.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐