private void button1_Click(object sender, EventArgs e) { try { Process p = new Process(); p.StartInfo.FileName = "cmd.exe "; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine( "ping 172.16.1.1 "); p.StandardInput.WriteLine( "exit "); string strRst = p.StandardOutput.ReadToEnd(); p.WaitForExit(); this.label1.Text = strRst; } catch (Exception err) { MessageBox.Show(err.Message); //显示错误信息。 } }