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

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.

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

DOS commands occur in "that" process. So, the cmdshell does accept the and act upon the CD directive, however, once that command shell closes, the process of changing directories has nothing to do with the next command shell directive because it would be in a new shell.

Maybe what you are looking for can be found at this URL
http://www.codeproject.com/KB/miscctrl/commandprompt.aspx

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

Dont forget then to mark this as solved

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.