title: How to run bat file with required permissions in C#
body: <p>I have a bat file that copies files from one location to another.</p>
  
  <pre><code>SET SRC=%1
  SET DEST=%2
  
  xcopy /Y/I %SRC%\*.txt %DEST%\temp
  echo Done!
  </code></pre>
  
  <p>I'm trying to run this file via C# program</p>
  
  <pre><code>var psi = new ProcessStartInfo(fileToRun);
  psi.Arguments = args;
  psi.RedirectStandardOutput = true;
  psi.RedirectStandardError = true;
  psi.WindowStyle = ProcessWindowStyle.Hidden;
  psi.UseShellExecute = false;
  psi.CreateNoWindow = true;
  
  Process cmdProc = Process.Start(psi); 
  
  StreamReader output = cmdProc.StandardOutput;
  StreamReader errors = cmdProc.StandardError;
  cmdProc.WaitForExit();
  </code></pre>
  
  <p>Bat-file is executed, I can see the <strong>'Done!'</strong> message in the output, but files are not copied.</p>
  
  <p>The only way it works is</p>
  
  <pre><code>psi.UseShellExecute = true;
  
  psi.RedirectStandardOutput = false;
  psi.RedirectStandardError = false;
  </code></pre>
  
  <p>But in this case I have to disable output/error redirection and I need them.
  So this doesn't work for me.</p>
  
  <p>I have tried to set administrator's username/password</p>
  
  <pre><code>psi.UserName = username;
  psi.Password = password; 
  </code></pre>
  
  <p>Logon succeed, but I get the <strong>'The handle is invalid'</strong> message in the StandardError Stream.</p>
  
  <p>I guess the process I'm trying to run doesn't have permissions to copy files and
  I don't know how to grant him these permissions.</p>
  
  <p>Please, help!</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>Thank you for replies! 
  I have spend several hours trying to handle this issue and as it always happens I have posted my question and found the solution :)</p>
  
  <p>In order to avoid getting 'The handle is invalid' message you have to</p>
  
  <pre><code>psi.RedirectStandardInput = true;
  </code></pre>
  
  <p><em><strong>But now I can see cmd.exe window, if UserName is set, which is bad.</em></strong></p>