3

I am opening the command prompt from c#

 Process.Start("cmd");

when it opens i need to write ipconfig automatically such that process opens and finds the ip of the workstation, How should i do that?

Sohail
  • 780
  • 3
  • 14
  • 27

4 Answers4

6

EDIT

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "ipconfig.exe";
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
return output;

or

EDIT

  Process pr = new Process();
  pr.StartInfo.FileName = "cmd.exe";
  pr.StartInfo.Arguments = "/k ipconfig"; 
  pr.Start();

Check : How to Execute a Command in C# ?

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
 startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 startInfo.FileName = "cmd.exe";
 startInfo.Arguments = "ipconfig";
 process.StartInfo = startInfo;
 process.Start(); 

or

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • this is not working, just the command promt is opening but its not writing ipconfig. i am nt hiding window. – Sohail Oct 08 '12 at 10:34
3

Try this

 string strCmdText; 
 strCmdText= "ipconfig";
 System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Ali Hasan
  • 1,045
  • 4
  • 16
  • 43
3

Use this

System.Diagnostics.Process.Start("cmd", "/K ipconfig");

This /K parameter will start cmd with an ipconfig command and will show it's output on the console too. To know more parameters which could be passed to a cmd Go here

yogi
  • 19,175
  • 13
  • 62
  • 92
  • Hi, how can I send multiple parameter, example `/K ipconfig` and `Pause` – Mowgli Mar 17 '13 at 21:44
  • 1
    @Mowgli try `/K ipconfig & Pause`, you can use `&` for combining commands. Read the provided link for more info. – yogi Mar 18 '13 at 11:31
1

there are specific ways to redirect standard input, standard output and error messages when running an external process like in your case, for example check here: ProcessStartInfo.RedirectStandardInput Property

then plenty of examples also here on SO: Sending input/getting output from a console application (C#/WinForms)

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147