title: How to kill remote desktop session for local computer only
body: <p>I'm using the following code to kill a remote desktop session and the application running in it. It works fine, the only problem is that it kills the specified application for all users.</p>
  
  <p>How do I keep this to just the local machine running a session?</p>
  
  <p>We have multiple users logging in and running this application from a server on their local machines. Most are running using work resources, but some use remote desktop.</p>
  
  <p>No matter how they are logged in when I run my code all users loose their sessions.</p>
  
  <pre><code>private void btnCloseSession_Click(object sender, EventArgs e)
      {
          if (!runningExclusiveProcess)
          {
              runningExclusiveProcess = true;
              btnCloseSession.Enabled = false;
                  //check and close Labware if running
                  if (chkCloseLabware.Checked == true) 
              {
                  if (chkExit.Checked == true)
                  {
                      KillLabWare();
                      Close();
                  }
                  else
                  {
                      KillLabWare();
                  }
              }
  
              Process[] my = Process.GetProcessesByName("mstsc");
  
              //loop thru list to get selected item(s)
              ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(lstOpenSessions);
              selectedItems = lstOpenSessions.SelectedItems;
  
              try
              {
                  //remove credentials
                  string szTestx = "/delete:GOJO.NET/" + cboServer.Text;
                  ProcessStartInfo infox = new ProcessStartInfo("cmdkey.exe", szTestx);
                  Process procx = new Process();
                  procx.StartInfo = infox;
                  procx.Start();
  
                  if (lstOpenSessions.SelectedIndex != -1)
                  {
                      for (int i = selectedItems.Count - 1; i &gt;= 0; i--)
                      {
                          //loop thru process to match process vs. list selection(s)
                          foreach (Process remote in my)
                          {
                              if (remote.MainWindowTitle == selectedItems[i].ToString())
                              {
                                  KillRS(remote.MainWindowTitle);
                                  lstOpenSessions.Items.Remove(selectedItems[i]);
                              }
                          }
  
                          if (lstOpenSessions.Items.Contains(selectedItems[i].ToString()))
                          {
                              lstOpenSessions.Items.Remove(selectedItems[i]);
                          }
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine("{0} Exception caught.", ex);
              }
              // If your task is synchronous, then undo your flag here:
              runningExclusiveProcess = false;
              btnCloseSession.Enabled = true;
          }
      }
      public void KillLabWare()
      {
          ConnectionOptions con = new ConnectionOptions();
          con.Username = cboUserName.Text;
          con.Password = txtPassWord.Text;
          string strIPAddress = cboServer.Text;
  
          ManagementScope scope = new
              ManagementScope(@"\\" + strIPAddress + @"\root\cimv2", con);
          scope.Connect();
          ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process WHERE Name='Labware.exe'");
          ManagementObjectSearcher searcher = new
              ManagementObjectSearcher(scope, query);
          ManagementObjectCollection objectCollection = searcher.Get();
          foreach (ManagementObject managementObject in objectCollection)
          {
              managementObject.InvokeMethod("Terminate", null);
          }
      }
      private void KillRS(string rwt)
      {
          foreach (Process p in Process.GetProcesses())
          {
              if (p.MainWindowTitle == rwt)
              {
                  p.Kill();
              }
          }
      }
      public static void KillRemoteProcess(Process p, string user, string password)
      {
          new Process
          {
              StartInfo = new ProcessStartInfo
              {
                  FileName = "TaskKill.exe",
                  Arguments = string.Format("/pid {0} /s {1} /u {2} /p {3}", p.Id, p.MachineName, user, password),
                  WindowStyle = ProcessWindowStyle.Hidden,
                  CreateNoWindow = true
              }
          }.Start();
      }
  </code></pre>