title: C# Sending a Server QUERY to CMD and Reading the Output
body: <p>UPDATE - My code now looks like this, and the file test.txt is generated after running, but the file is empty, so I'm assuming the command is not being sent to the CMD properly:</p>
  
  <pre><code>    static void command()
      {
          string cmd = "/c QUERY session &gt;C:/test.txt";
  
          Process proc = new Process();
  
          proc.StartInfo.LoadUserProfile = true;
  
          proc.StartInfo.FileName = "cmd.exe";
          proc.StartInfo.Arguments = cmd;
  
          proc.Start();
          proc.WaitForExit();    
      }
  </code></pre>
  
  <p>Can anyone think of a way for me to debug this?
  Visual Studio shows no errors so it is hard to tell what is going on.</p>
  
  <p>Any help is much appreciated!</p>
  
  <hr>
  
  <p>After having a look around on StackExchange, I have ended up with the following code for opening the command prompt and sending a command:</p>
  
  <pre><code>        string cmd = "/c QUERY user /SERVER: servername.goes.here";
          Process proc = new Process();
          proc.StartInfo.FileName = "cmd.exe";
          proc.StartInfo.Arguments = cmd;
          proc.StartInfo.UseShellExecute = false;
          proc.StartInfo.RedirectStandardOutput = true;
          proc.Start();
  
          string output = proc.StandardOutput.ReadToEnd();
  
          MessageBox.Show(output);
  
          proc.WaitForExit();
  </code></pre>
  
  <p>When replacing the Server Query with a simple "<code>notepad.exe</code>", CMD window opens when running, and so does <code>notepad.exe</code>.</p>
  
  <p>However, when sending the Server query (This is to check which users are using a particular Citrix server), I am seeing this message in the CMD:</p>
  
  <blockquote>
    <p>'QUERY' is not recognized as an internal or external command, operable
    program or batch file.</p>
  </blockquote>
  
  <p>When I manually type the query into CMD, I am presented with a list of all users currently logged in to that particular server. </p>
  
  <p>What I would like for the program to run the SERVER query, and send the output to a message window, so that I know it has worked.</p>
  
  <p>Any help with this is much appreciated!</p>
  
  <p>Many thanks</p>
  
  <p>I also noticed that when removing the <code>/c</code> from the cmd String, I don't see a query error in the CMD, but seeing no output either here or in the message box</p>