在打开进程启动命令提示符后,如何在C#中编写ipconfig

3
我正在使用c#打开命令提示符。
 Process.Start("cmd");

当它打开时,我需要自动写入ipconfig,以便该进程打开并找到工作站的IP地址。我应该怎么做?

如果您需要查找本地机器的 IP 地址,有比启动 ipconfig 更好的方法。 - Damien_The_Unbeliever
4个回答

6

编辑

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;

或者

编辑

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

检查: 如何在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(); 

或者

这不起作用,只是打开了命令提示符但没有写入ipconfig。我没有隐藏窗口。 - Sohail

3
尝试这个。
 string strCmdText; 
 strCmdText= "ipconfig";
 System.Diagnostics.Process.Start("CMD.exe",strCmdText);

3
请使用此功能。
System.Diagnostics.Process.Start("cmd", "/K ipconfig");

这个 /K 参数将使用 ipconfig 命令启动cmd并在控制台上显示其输出。 了解更多可传递到 cmd 的参数,请点击此处


你好,我该如何发送多个参数,例如 /K ipconfigPause - Mowgli
1
@Mowgli 试试 /K ipconfig & Pause,你可以使用 & 来组合命令。阅读提供的链接获取更多信息。 - yogi

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接