0

Hi there,

Can C# code , execute commands we write in the cmd window ?
i.e: is there a way to let a C# application interact in somehow the command prompt?

example : we use the command prompt line: ipconfig to see info about the IP address of the machine, can I execute this command from the C#? (I now that there is a way in C# to get the IP address but this is not what I want , the ip address is just an example)

Thanks in advance

4
Contributors
6
Replies
7
Views
10 Years
Discussion Span
Last Post by knowledgelover
0

Here is an example. You can also tack on a p.WaitForExit(); if needed.

using System.Diagnostics;
.
.
.
            Process p = new Process();
            p.StartInfo.FileName = "IPCONFIG";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.Arguments = "/all";
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            textBox1.Text = p.StandardOutput.ReadToEnd();

// Jerry
if this solves your issue, please remember to mark it as solved.

0

Here is an example. You can also tack on a p.WaitForExit(); if needed.

using System.Diagnostics;
.
.
.
            Process p = new Process();
            p.StartInfo.FileName = "IPCONFIG";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.Arguments = "/all";
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            textBox1.Text = p.StandardOutput.ReadToEnd();

is this supose to work with all kind of commands , like cd , when I tried it, I did not see the folder opened , i.e there are some commands has no return output but has actions , what should I do to see them ,

Thanks

0

using System.Diagnostics;
.
.
.
Process p = new Process();
p.StartInfo.FileName = "IPCONFIG";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/all";
p.StartInfo.RedirectStandardOutput = true;
p.Start();
textBox1.Text = p.StandardOutput.ReadToEnd();

This code Works Fantastically fulfilling my present need...
Thank u Jerry

0

Thank you Jerry
it was really helpful
and I got the needed work done :)
thanks again

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.