你如何杀死一个进程在.NET(C#)一个特定的用户? [英] How do you kill a process for a particular user in .NET (C#)?

查看:119
本文介绍了你如何杀死一个进程在.NET(C#)一个特定的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作掀起了多用户的Windows Server,以及错误RDPCLIP每天咬我们。我们通常只需打开任务管理器,并杀死然后重新启动RDPCLIP,但这是在一个痛苦的对接。我写了一个PowerShell脚本进行查杀,然后重新启动RDPCLIP,但没有人使用它,因为它是一个脚本(更不用说执行政策受到限制的框)。我试图写一个快速和肮脏的Windows应用程序,你点击一个按钮,杀死RDPCLIP并重新启动它。但我想它限制在当前用户,并不能找到,这是否Process类的方法。到目前为止,这是我有:

I work off of a multi-user Windows Server, and the rdpclip bug bites us all daily. We usually just open task manager and kill then restart rdpclip, but that's a pain in the butt. I wrote a powershell script for killing then restarting rdpclip, but no one's using it because it's a script (not to mention the execution policy is restricted for the box). I'm trying to write a quick and dirty windows app where you click a button to kill rdpclip and restart it. But I want to restrict it to the current user, and can't find a method for the Process class that does this. So far, here's what I have:

Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist)
{
    if (theprocess.ProcessName == "rdpclip")
    {
      theprocess.Kill();
      Process.Start("rdpclip");
    }
}

我不能肯定,但我认为这将杀死所有的进程RDPCLIP。我想通过用户选择,比如我的PowerShell脚本的作用:

I'm not certain, but I think that's going to kill all the rdpclip processes. I'd like to select by user, like my powershell script does:

taskkill /fi "username eq $env:username" /im rdpclip.exe
& rdpclip.ex

我想我可以从我的可执行文件调用PowerShell脚本,但似乎相当缺憾。

I suppose I could just invoke the powershell script from my executable, but that seems fairly kludgy.

在提前任何格式问题道歉,这是我第一次来这里。

Apologies in advance for any formatting issues, this is my first time here.

更新:我还需要知道如何获得当前用户,只有选择那些过程。下面提出的WMI解决方案不帮我拿的。

UPDATE: I also need to know how to get the current user and select only those processes. The WMI solution proposed below doesn't help me get that.

UPDATE2:好吧,我已经找到了如何获取当前用户,但它不匹配通过远程桌面的过程中用户。任何人都知道怎么去,而不是SID用户名?

UPDATE2: Ok, I've figured out how to get the current user, but it doesn't match the process user over Remote Desktop. Anyone know how to get username instead of the SID?

干杯, fr0man

Cheers, fr0man

推荐答案

好了,这里就是我最后做:

Ok, here's what I ended up doing:

           Process[] processlist = Process.GetProcesses();
            bool rdpclipFound = false;

            foreach (Process theprocess in processlist)
            {
                String ProcessUserSID = GetProcessInfoByPID(theprocess.Id);
                String CurrentUser = WindowsIdentity.GetCurrent().Name.Replace("SERVERNAME\\",""); 

                if (theprocess.ProcessName == "rdpclip" && ProcessUserSID == CurrentUser)
                {
                    theprocess.Kill();
                    rdpclipFound = true;
                }

            }
            Process.Start("rdpclip");
            if (rdpclipFound)
            {
               MessageBox.Show("rdpclip.exe successfully restarted"); }
            else
            {
               MessageBox.Show("rdpclip was not running under your username.  It has been started, please try copying and pasting again.");
            }

            }

这篇关于你如何杀死一个进程在.NET(C#)一个特定的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆