Command line in C# Help

Does anyone know what function to call to run a command line?
Like I'd like to run a command "dir".

And can you please email me to: vnt2@yahoo.com

Thank you very much
Trevor V

Comments

  • : Does anyone know what function to call to run a command line?
    : Like I'd like to run a command "dir".
    :
    : And can you please email me to: vnt2@yahoo.com
    :
    : Thank you very much
    : Trevor V
    :
    Its very simple to run CMD.EXE using the Process class.

    Process.Start("CMD.EXE", "/c dir");

    However the command window will close immediatly after completing the task. This might be ok if you just want to run something but if you want some output you will have to complicate things a bit :)

    So this is what I do to run something from the command line and recieve all of the output in the form of a string:

    StreamWriter sw; // Handles strings sent to CMD.exe
    StreamReader sr; // Reads text back from CMD.exe
    StreamReader err; // Returns all errors from CMD.exe
    Process dir = new Process(); // Will run CMD.EXE for you

    dir.StartInfo.FileName = "CMD.EXE";
    dir.StartInfo.UseShellExecute = false;
    dir.StartInfo.CreateNoWindow = true;
    dir.StartInfo.RedirectStandardInput = true;
    dir.StartInfo.RedirectStandardError = true;
    dir.StartInfo.RedirectStandardOutput = true;

    dir.Start(); // Runs CMD.exe
    sw = dir.StandardInput;
    sr = dir.StandardOutput;
    err = dir.StandardError;
    sw.AutoFlush = true;
    sw.WriteLine("dir"); // Sends strings to CMD.EXE
    sw.Close();
    MessageBox.Show(sr.ReadToEnd().ToString()); // Get your output :)
    sr.Close();
    err.Close();

    You can incorporate the 'err' if you think an error could happen.

    E-Mail has been sent.
  • Trever,

    The reply to your message did indeed contain valuable information. However, I just wanted to add that if your sole objective was keeping the console 'in sight' and seeing the result of your 'dir', then just swap the '/C' in "Process.Start("CMD.EXE", "/c dir")" with '/K'.

    Hope this helps,
    L.


    : : Does anyone know what function to call to run a command line?
    : : Like I'd like to run a command "dir".
    : :
    : : And can you please email me to: vnt2@yahoo.com
    : :
    : : Thank you very much
    : : Trevor V
    : :
    : Its very simple to run CMD.EXE using the Process class.
    :
    : Process.Start("CMD.EXE", "/c dir");
    :
    : However the command window will close immediatly after completing the task. This might be ok if you just want to run something but if you want some output you will have to complicate things a bit :)
    :
    : So this is what I do to run something from the command line and recieve all of the output in the form of a string:
    :
    : StreamWriter sw; // Handles strings sent to CMD.exe
    : StreamReader sr; // Reads text back from CMD.exe
    : StreamReader err; // Returns all errors from CMD.exe
    : Process dir = new Process(); // Will run CMD.EXE for you
    :
    : dir.StartInfo.FileName = "CMD.EXE";
    : dir.StartInfo.UseShellExecute = false;
    : dir.StartInfo.CreateNoWindow = true;
    : dir.StartInfo.RedirectStandardInput = true;
    : dir.StartInfo.RedirectStandardError = true;
    : dir.StartInfo.RedirectStandardOutput = true;
    :
    : dir.Start(); // Runs CMD.exe
    : sw = dir.StandardInput;
    : sr = dir.StandardOutput;
    : err = dir.StandardError;
    : sw.AutoFlush = true;
    : sw.WriteLine("dir"); // Sends strings to CMD.EXE
    : sw.Close();
    : MessageBox.Show(sr.ReadToEnd().ToString()); // Get your output :)
    : sr.Close();
    : err.Close();
    :
    : You can incorporate the 'err' if you think an error could happen.
    :
    : E-Mail has been sent.
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories