title: How to capture Shell command output in C#?
body: <p>Summary:</p>
  
  <ul>
  <li>query registry on remote machine</li>
  <li>capture output to use in application</li>
  <li>needs to be in csharp</li>
  <li>so far all methods used can only query on the local machine</li>
  <li>any hope is greatly appreciated</li>
  </ul>
  
  <p>Full issue:</p>
  
  <p>I need to find a way to run a commandline command in csharp and capture its output. I know how to do this in Perl, below is the code I would use in Perl.</p>
  
  <pre><code>#machine to check
  my $pc = $_[0];
  #create location of registry query
  my $machine = "\\\\".$pc."\\HKEY_USERS";
  #run registry query
  my @regQuery= `REG QUERY $machine`;
  </code></pre>
  
  <p>Any suggestions on how to do this in csharp would be welcome. So far ive tried using the RegistryKey OurKey = Registry.Users method and it works great but i can not query the registry on a remote machine.</p>
  
  <p>Please let me know if you need any more information.</p>
  
  <p>SOLUTION:(Thank you to @Robaticus)</p>
  
  <pre><code>private void reg(string host)
          {
  
              string build = "QUERY \\\\" + host + "\\HKEY_USERS";
              string parms = @build;
              string output = "";
              string error = string.Empty;
  
              ProcessStartInfo psi = new ProcessStartInfo("reg.exe", parms);
  
              psi.RedirectStandardOutput = true;
              psi.RedirectStandardError = true;
              psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
              psi.UseShellExecute = false;
              System.Diagnostics.Process reg;
              reg = System.Diagnostics.Process.Start(psi);
              using (System.IO.StreamReader myOutput = reg.StandardOutput)
              {
                  output = myOutput.ReadToEnd();
              }
              using (System.IO.StreamReader myError = reg.StandardError)
              {
                  error = myError.ReadToEnd();
  
              }
              Output.AppendText(output + "\n");
  
  
          }  
  </code></pre>