我正在使用c#打开命令提示符。
当它打开时,我需要自动写入ipconfig,以便该进程打开并找到工作站的IP地址。我应该怎么做?
Process.Start("cmd");
当它打开时,我需要自动写入ipconfig,以便该进程打开并找到工作站的IP地址。我应该怎么做?
Process.Start("cmd");
编辑
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();
string strCmdText;
strCmdText= "ipconfig";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
System.Diagnostics.Process.Start("cmd", "/K ipconfig");
这个 /K
参数将使用 ipconfig
命令启动cmd并在控制台上显示其输出。
了解更多可传递到 cmd 的参数,请点击此处
/K ipconfig
和 Pause
? - Mowgli/K ipconfig & Pause
,你可以使用 &
来组合命令。阅读提供的链接获取更多信息。 - yogi在运行外部进程时,有特定的方法来重定向标准输入、标准输出和错误消息,例如在您的情况下,请参考此处:ProcessStartInfo.RedirectStandardInput Property
然后在这里也有很多例子:Sending input/getting output from a console application (C#/WinForms)。