locked
Running command line commands from code in C# RRS feed

  • Question

  •  

    I am trying to write a program that is capable of executing commands in command line as well as read the results.

    But my code does not work.

    Here is my code:


     System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo();
                sinf.FileName = @"C:\WINDOWS\system32\cmd.exe";
                sinf.Arguments = @"query process iscte.exe";
                sinf.RedirectStandardOutput = true;
                sinf.UseShellExecute = false;
                sinf.CreateNoWindow = true;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = sinf;
                p.Start();
                string teste = "";
                string res = p.StandardOutput.ReadToEnd();

    • Moved by Peter Ritchie Friday, June 20, 2008 5:03 PM Question relates to class in the BCL
    Friday, May 16, 2008 9:27 AM

Answers

  •  

    I am trying to write a program that is capable of executing commands in command line as well as read the results.

    But my code does not work.

    Here is my code:

               System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo();
                sinf.FileName = @"C:\WINDOWS\system32\cmd.exe";
                sinf.Arguments = @"query process iscte.exe";
                sinf.RedirectStandardOutput = true;
                sinf.UseShellExecute = false;
                sinf.CreateNoWindow = true;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = sinf;
                p.Start();
                string teste = "";
                string res = p.StandardOutput.ReadToEnd();

    • Merged by Peter Ritchie Friday, June 20, 2008 5:03 PM duplicate
    • Marked as answer by timSousa Tuesday, October 21, 2008 2:26 PM
    Friday, May 16, 2008 9:31 AM
  •  

    I ran the command 'query process iscte.exe' from the command line and got 'No Process exists for iscte.exe' to at least I could confirm the command should work.

     

    I then changed how the process is being started by modifying the FileName and Parameters properties like this:

     

    Code Snippet

    System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo();

    sinf.FileName = @"query ";

    sinf.Arguments = @"process iscte.exe";

    sinf.RedirectStandardError = true;

    sinf.RedirectStandardOutput = true;

    sinf.UseShellExecute = false;

    sinf.CreateNoWindow = true;

    System.Diagnostics.Process p = new System.Diagnostics.Process();

    p.StartInfo = sinf;

    p.Start();

    string teste = "";

    string res = p.StandardOutput.ReadToEnd();

    string errors = p.StandardError.ReadToEnd();

    MessageBox.Show("result: " + res);

    MessageBox.Show("error: " + errors);

     

     

    As you can see I've set the RedirectStandardError property to true so I can get to the StandardError output.

     

    I've changed the Filename property to "query" and the Arguments property to "process iscte.exe" which removes the execution of cmd.exe from the code as I suggested in my previous post. It now seems to work as you might expect.

     

    In my case if I change the Arguments property to "process explorer.exe", res gets populated with the following:

     

    Code Snippet

    " USERNAME SESSIONNAME ID PID IMAGE\r\n>jim console 1 2848 explorer.exe\r\n"

     

     

    Hopefully this is what you were looking for!

     

    Best

     

    J

     

     

     

    Friday, May 16, 2008 10:39 AM

All replies

  •  

    I am trying to write a program that is capable of executing commands in command line as well as read the results.

    But my code does not work.

    Here is my code:

               System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo();
                sinf.FileName = @"C:\WINDOWS\system32\cmd.exe";
                sinf.Arguments = @"query process iscte.exe";
                sinf.RedirectStandardOutput = true;
                sinf.UseShellExecute = false;
                sinf.CreateNoWindow = true;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = sinf;
                p.Start();
                string teste = "";
                string res = p.StandardOutput.ReadToEnd();

    • Merged by Peter Ritchie Friday, June 20, 2008 5:03 PM duplicate
    • Marked as answer by timSousa Tuesday, October 21, 2008 2:26 PM
    Friday, May 16, 2008 9:31 AM
  •  

    Hi Tim,

     

    Are you getting an error or is your code just not working as expected?

     

    My initial thoughts are that you probably don't need to execute cmd.exe for this - you should be able to specify the executable you want to execute as the FileName and pass the parameters using the Arguments property. I have certainly had problems running processes when I've specified the use of cmd.exe before.

     

    If you can post more information I'll take a look for you.

     

    Best

     

    J

     

    Friday, May 16, 2008 10:02 AM
  • Hello, my code just not working as expected.
    Friday, May 16, 2008 10:10 AM
  •  

    I ran the command 'query process iscte.exe' from the command line and got 'No Process exists for iscte.exe' to at least I could confirm the command should work.

     

    I then changed how the process is being started by modifying the FileName and Parameters properties like this:

     

    Code Snippet

    System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo();

    sinf.FileName = @"query ";

    sinf.Arguments = @"process iscte.exe";

    sinf.RedirectStandardError = true;

    sinf.RedirectStandardOutput = true;

    sinf.UseShellExecute = false;

    sinf.CreateNoWindow = true;

    System.Diagnostics.Process p = new System.Diagnostics.Process();

    p.StartInfo = sinf;

    p.Start();

    string teste = "";

    string res = p.StandardOutput.ReadToEnd();

    string errors = p.StandardError.ReadToEnd();

    MessageBox.Show("result: " + res);

    MessageBox.Show("error: " + errors);

     

     

    As you can see I've set the RedirectStandardError property to true so I can get to the StandardError output.

     

    I've changed the Filename property to "query" and the Arguments property to "process iscte.exe" which removes the execution of cmd.exe from the code as I suggested in my previous post. It now seems to work as you might expect.

     

    In my case if I change the Arguments property to "process explorer.exe", res gets populated with the following:

     

    Code Snippet

    " USERNAME SESSIONNAME ID PID IMAGE\r\n>jim console 1 2848 explorer.exe\r\n"

     

     

    Hopefully this is what you were looking for!

     

    Best

     

    J

     

     

     

    Friday, May 16, 2008 10:39 AM
  • remember, if you are directly invoking cmd.exe from your Process arguments, it will not accept any arguments with it - not sure why but it completely ignores it. So try to directly execute your program that you want to execute, and not through using cmd.exe

    Friday, May 16, 2008 12:01 PM
  • Why you need "CMD.EXE"? The cmd program will keep waiting for the "exit" command fto quit, so if you manually type "exit" it will exit and you will get the result. again is the argument @"query process iscte.exe" is correct?


    Following code shows an example for the "ping" utility instead of "cmd". (Remember ping exits automatically for normal run).


    Code Snippet

    System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo();

    sinf.FileName = "ping";

              Console.WriteLine(res);

    sinf.Arguments = "192.168.1.1";

              sinf.RedirectStandardOutput = true;
              sinf.UseShellExecute = false;
              //sinf.CreateNoWindow = true;
              System.Diagnostics.Process p = new System.Diagnostics.Process();
              p.StartInfo = sinf;
              p.Start();
              string res = p.StandardOutput.ReadToEnd();

             
    Friday, May 16, 2008 7:07 PM
  • Thanks for this, was taking me ages to fix, your code worked a treat in the end. Thanks a million. I was so pleased I made it into a class, if you are running a command that takes no file name just pass it "".

    public void RunCommand(string commandToRun, string args, string workingDirectory, string workingFilename)
            { 
                System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo(); 
     
                sinf.FileName = commandToRun; 
     
                sinf.Arguments = args + " " + workingFilename; 
     
                sinf.WorkingDirectory = workingDirectory; 
     
                sinf.RedirectStandardError = true
     
                sinf.RedirectStandardOutput = true
     
                sinf.UseShellExecute = false
     
                sinf.CreateNoWindow = true
     
                System.Diagnostics.Process p = new System.Diagnostics.Process(); 
     
                p.StartInfo = sinf; 
     
                p.Start(); 
     
                string res = p.StandardOutput.ReadToEnd(); 
     
                string errors = p.StandardError.ReadToEnd(); 
     
                MessageBox.Show(res); 
     
                MessageBox.Show(errors);
              
                p.Close();

    • Edited by d347hm4n Monday, July 14, 2008 1:24 PM had to included a space beetween args and workingfileName
    • Proposed as answer by d347hm4n Tuesday, October 21, 2008 2:32 PM
    Monday, July 14, 2008 1:01 PM