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.
How to get result from the console?
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mike GoldRank #50 · 30.6K pointsPosted Jul 30, 2007, 7:41 AM EDT
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
Eric13 pointsPosted Jul 30, 2007, 10:40 AM EDT
Thanks alot for the help! :)
I hope it can work under linux/unix too, can it?
Mike GoldRank #50 · 30.6K pointsPosted Jul 30, 2007, 11:34 AM EDT
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.
Eric13 pointsPosted Jul 31, 2007, 7:15 AM EDT
Thank you very much again, Mike, you saved my world.