First, I use my own Process Wrapper to keep the original process path.
public class MCProcess() { public Process Process { get; set;} public string StartingPath { get; set;} public MCProcess(string start, Process p) { Process = p; StartingPath = start; } }
Now I have a List<MCProcces> called runProcesses, which I use to track all the processes and start paths for every process that my program started.
Example:
string path = "C:\\Windows\\System32\\notepad.exe"; Process temp = Process.Start(path); runningProcesses.Add(new MCProcess(path, temp));
Now, sometimes, I want to close the processes that I performed. Instead of looking at the task manager and trying to find MainModuleName for every process that I started, I included StartPath for some reason.
If I want to close the notepad, I just go through my running processes, find out which process has the initial notepad for the notepad, and then use Process.Kill to kill this process.
string path = "C:\\Windows\\System32\\notepad.exe"; for (int i = 0; i < runningProcesses.Count; i++) { if (runningProcesses[i].StartingPath == path) { runningProcesses[i].Process.Kill(); runningProcesses.RemoveAt(i); } }
This code works fine on Windows 7, and I had no problems at all. However, when using this on Windows XP, I get an ArgumentNullException with Process.Kill.
Is there something in the Process class that doesn't make it work well in Windows XP?
source share