//摘录:《程序员秘书》--源代码--进程--后台执行DOS命令 private void button1_Click(object sender, EventArgs e) { Process myProcess = new Process(); myProcess.StartInfo.FileName = "cmd.exe ";//DOS控制平台 myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardError = true; myProcess.Start(); StreamWriter sIn = myProcess.StandardInput;//标准输入流 sIn.AutoFlush = true; StreamReader sOut = myProcess.StandardOutput;//标准输入流 StreamReader sErr = myProcess.StandardError;//标准错误流 sIn.Write( "ver " + System.Environment.NewLine);//DOS控制平台上的命令 sIn.Write( "dir " + System.Environment.NewLine);//DOS控制平台上的命令 sIn.Write( "exit " + System.Environment.NewLine); string s = sOut.ReadToEnd();//读取执行DOS命令后输出信息 string er = sErr.ReadToEnd();//读取执行DOS命令后错误信息 richTextBox1.AppendText(s); richTextBox1.AppendText(er); if (myProcess.HasExited == false) { myProcess.Kill(); } sIn.Close(); sOut.Close(); sErr.Close(); myProcess.Close(); }