Skip to content

How to get result from the console?

Eric

Asked Jul 30, 2007, 6:20 AM EDT4 answers
Share
LinkedInX
I am wondering if I can pass a command such as "ipconfig /all" in C# to the windows, and get the result back in some variable/array? If so, how?

Thanks.

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    Hi Eric,

    You can redirect all your standard output into a string.  Then you can parse the ipconfig info out of the string into an array:

    Process process = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.RedirectStandardOutput = true;
    info.FileName = "ipconfig.exe";
    info.Arguments = "/all";
    info.UseShellExecute = false;
    process.StartInfo = info;
    process.Start();

    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    // do parsing of output string here

    0
  • 0
    Hi Mike,
    Thanks alot for the help! :)
    I hope it can work under linux/unix too, can it?
    0
  • Mike Gold
    0
    Hi Eric,

    If you want to run the code above using Linux, you'll probably need to use the MONO library for .NET.  Microsoft doesn't currently support Linux and Unix, but there is an open source library (mono) that works with Linux.
    0
  • 0
    Got cha! XD

    Thank you very much again, Mike,  you saved my world.
    0