title: Writing a cmd Command in a Console Application
body: <p>I'm trying to write a C# console application that will launch <code>runas.exe</code> through cmd then run another application as that user.  I've taken one of the suggestions listed below (and added a little bit) as it seems the most promising.</p>
  
  <pre><code>Process cmd = new Process();
  ProcessStartInfo startinfo = new ProcessStartInfo("cmd.exe", @"/K C:\Windows\System32\runas.exe /noprofile /user:DOMAIN\USER'c:\windows\system32\notepad.exe\'")
  {
      RedirectStandardInput = true,
      UseShellExecute = false
  };
  cmd.StartInfo = startinfo;
  cmd.Start();
  StreamWriter stdInputWriter = cmd.StandardInput;
  stdInputWriter.Write("PASSWORD");
  cmd.WaitForExit();
  </code></pre>
  
  <p>When I launch the application it asks for the password before the command itself causing there to be an error with <code>runas.exe</code></p>
  
  <p><a href="https://i.stack.imgur.com/AfJs1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AfJs1.png" alt="enter image description here"></a></p>
  
  <p>I'm pretty sure <code>UseShellExecute = false</code> is causing the error but the <code>StreamWriter</code> doesn't work without it so I'm not sure what to do.</p>