Howdy, Stranger!

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

Categories

Command line in C# Help

VNTVNT Member Posts: 1
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

  • JCrouchJCrouch Member Posts: 9
    : 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.
  • Loai NaamaniLoai Naamani Member Posts: 1
    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.
    :

  • Andre YoungAndre Young USAMember Posts: 0

    ____ < http://forcoder.org /> free video tutorials and ebooks about || Java C++ C MATLAB Go Swift Delphi Assembly JavaScript Scratch Perl Objective-C Visual Basic .NET PL/SQL Python Ruby C# PHP R Visual Basic Scala COBOL Transact-SQL Erlang Kotlin Apex D Rust SAS VBScript Crystal Logo Clojure LabVIEW Scheme Lisp Hack Lua Ada F# Alice FoxPro Prolog Fortran ABAP Julia ML Bash Awk Dart || ________

Sign In or Register to comment.