繁体   English   中英

如何在 .NET (C#) 中杀死特定用户的进程?

[英]How do you kill a process for a particular user in .NET (C#)?

我在多用户 Windows Server 上工作,rdpclip 错误每天都在折磨我们。 我们通常只是打开任务管理器并杀死然后重新启动 rdpclip,但这很麻烦。 我写了一个powershell脚本来杀死然后重新启动rdpclip,但没有人使用它,因为它是一个脚本(更不用说盒子的执行策略受到限制)。 我正在尝试编写一个快速而肮脏的 Windows 应用程序,您可以在其中单击一个按钮来终止 rdpclip 并重新启动它。 但是我想将其限制为当前用户,并且找不到执行此操作的 Process 类的方法。 到目前为止,这就是我所拥有的:

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

我不确定,但我认为这会杀死所有 rdpclip 进程。 我想按用户选择,就像我的 powershell 脚本一样:

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

我想我可以从我的可执行文件中调用 powershell 脚本,但这似乎相当笨拙。

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

更新:我还需要知道如何获取当前用户并仅选择那些进程。 下面提出的 WMI 解决方案并不能帮助我做到这一点。

UPDATE2:好的,我已经想出了如何获取当前用户,但它与远程桌面上的进程用户不匹配。 有谁知道如何获取用户名而不是 SID?

干杯,fr0man

我没有使用 GetProcessInfoByPID,而是从 StartInfo.EnvironmentVariables 中获取数据。

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Security.Principal;
using System.Runtime.InteropServices;

namespace KillRDPClip
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] processlist = Process.GetProcesses();
            foreach (Process theprocess in processlist)
            {
                String ProcessUserSID = theprocess.StartInfo.EnvironmentVariables["USERNAME"];
                String CurrentUser = Environment.UserName;
                if (theprocess.ProcessName.ToLower().ToString() == "rdpclip" && ProcessUserSID == CurrentUser)
                {
                    theprocess.Kill();
                }
            }
        }
    }
}

好的,这是我最终做的:

           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.");
            }

            }

阅读以下 CodeProject 文章,它包含您需要的所有信息:

如何获取进程所有者 ID 和当前用户 SID

您可以在隐藏模式下打开 cmd 并终止特定于用户的进程。 在这里,我试图杀死特定于当前用户的 Excel 进程:

            String CurrentUser = Environment.UserName;
            Process[] allExcelProcesses = Process.GetProcessesByName("excel");
            if (null != allExcelProcesses)
            {
                Process process = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = "cmd.exe";
                startInfo.Arguments = "/C TASKKILL /F /FI \"USERNAME eq " + CurrentUser + "\" /IM EXCEL.EXE";
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM