title: programmatically kill a process in vista/windows 7 in C#
body: <p>I want to kill a process programmatically in vista/windows 7 (I'm not sure if there's significant problems in the implementation of the UAC between the two to make a difference).  </p>
  
  <p>Right now, my code looks like:</p>
  
  <pre><code>  if(killProcess){
        System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("MyProcessName");
         // Before starting the new process make sure no other MyProcessName is running.
          foreach (System.Diagnostics.Process p in process)
          {
              p.Kill();
          }
  
          myProcess = System.Diagnostics.Process.Start(psi);
     }
  </code></pre>
  
  <p>I have to do this because I need to make sure that if the user crashes the program or exits abruptly, this secondary process is restarted when the application is restarted, or if the user wants to change the parameters for this secondary process.  </p>
  
  <p>The code works fine in XP, but fails in Windows 7 (and I assume in Vista) with an 'access is denied' message.  From what the Almighty Google has told me, I need to run my killing program as administrator to get around this problem, but that's just weak sauce.  The other potential answer is to use LinkDemand, but I don't understand the msdn page for LinkDemand as it pertains to processes.</p>
  
  <p>I could move the code into a thread, but that has a whole host of other difficulties inherent to it that I really don't want to discover.</p>