我试过了,这样没问题啊
ProcessStartInfo si = new ProcessStartInfo("ping", "www.163.com");
si.CreateNoWindow = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
textBox1.Text = p.StandardOutput.ReadToEnd();
ProcessStartInfo si = new ProcessStartInfo("ping", "www.163.com");
si.CreateNoWindow = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
textBox1.Text = p.StandardOutput.ReadToEnd();
如:dir > a.txt
感觉好像与命令是内部或外部命令有关.因为前时间想获取net use命令的结果也没有成功,但是ping命令却可以.期待此贴有解,从中学习.
{
Process pCmd = new Process();
pCmd.StartInfo.FileName = "cmd.exe"; pCmd.StartInfo.UseShellExecute = false;
pCmd.StartInfo.RedirectStandardInput = true;
pCmd.StartInfo.RedirectStandardOutput = true;
pCmd.StartInfo.RedirectStandardError = true;
pCmd.StartInfo.CreateNoWindow = true;
pCmd.Start(); pCmd.StandardInput.WriteLine("DOS程序");
StreamReader srTemp = pCmd.StandardOutput;
StringBuilder sbTemp = new StringBuilder();
while (srTemp.ReadLine()!= null)
{
sbTemp.Remove(0, sbTemp.Length);
sbTemp.Append(srTemp.ReadLine());
Console.WriteLine(sbTemp.ToString()); //Add output to console
}
}protected static void GetPingInfo()
{
ProcessStartInfo si = new ProcessStartInfo("ping", "www.163.com");
si.CreateNoWindow = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
string str = p.StandardOutput.ReadToEnd();
Console.Write(str); //Add output to console }
ProcessStartInfo si = new ProcessStartInfo("netsh");
si.CreateNoWindow = true;
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
p.StandardInput.WriteLine("show");
p.StandardInput.WriteLine("exit");
textBox1.Text = p.StandardOutput.ReadToEnd();
行方式为:cmd arg1,其中arg1为参数,实际是保存在cmd所在目录下的一个文件。