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

I am working with a multi-user Windows Server and the rdpclip error will bite us all daily. Usually we just open the task manager and kill, and then restart rdpclip, but this is a pain in the butt. I wrote a powershell script to kill and then restarted rdpclip, but no one used it because it is a script (not to mention the execution policy for this field). I am trying to write a quick and dirty Windows application where you press a button to kill rdpclip and restart it. But I want to limit it to the current user and cannot find a method for the Process class that does this. So far I have:

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

I'm not sure, but I think I will kill all the rdpclip processes. I would like to select a user, for example, my powershell script does:

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

I suppose I could just call the powershell script from my executable, but that seems pretty awkward.

Sorry 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 below does not help me with this.

UPDATE2: Well, I figured out how to get the current user, but it does not match the process user via Remote Desktop. Does anyone know how to get username instead of SID?

Cheers fr0man

+5
source share
3 answers

Ok, here is what I did:

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

            }
+6

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

Read the following CodeProject article, it contains all the necessary information:

How to get the process owner ID and current user SID

+1
source

All Articles