Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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

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

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.

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: 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?

Cheers, fr0man

like image 306
fr0man Avatar asked Jan 09 '09 00:01

fr0man


People also ask

How do you close a specific process in C#?

You can use Process. GetProcesses() to get the currently running processes, then Process. Kill() to kill a process.

How do I kill a process in Visual Studio?

Killing a specific active network processOpen up the prompt ( F1 \ Ctrl+Shift+P \ CMD+Shift+P ) and select: "Task Kill: Kill an active network task (by port number)" or simply hit Ctrl + Del (numpad_decimal). Type in the port number of the process you want to kill. Hit enter.


1 Answers

Instead of using GetProcessInfoByPID, I just grab the data from 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();
                }
            }
        }
    }
}
like image 78
Bentley Avatar answered Nov 09 '22 15:11

Bentley