According to this response in the OS can do this:
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.");
}
I put in the Github for future reference.
Obviously it needs some adaptations. There is fixed what the process is. But I think you can see where he takes the user’s information and compares it to see if it’s from the user or not and if he should kill him. There is another answer that does not use the current user. And has link for other solutions.
Another solution closer to what you need with another approach in another response in the OS.
static void KillProcessByNameAndUserName(string processName, string userName) {
var processes = from p in Process.GetProcessesByName(processName)
where GetProcessOwner(p.Id) == userName
select p;
foreach(Process p in processes) p.Kill();
}
static string GetProcessOwner(int processId) {
string query = “Select * From Win32_Process Where ProcessID = “ + processId;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList) {
string[] argList = new string[] { string.Empty };
int returnVal = Convert.ToInt32(obj.InvokeMethod(“GetOwner”, argList));
if (returnVal == 0)
return argList[0];
}
return “NO OWNER”;
}
I put in the Github for future reference.