程序如下,和网上的一样:
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.StandardInput.WriteLine("D:");
p.WaitForExit();
p.Close();
启动调试后Cmd打开但什么内容都没有,也不能输入,只能关闭。把p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;全部删掉才行,但p.StandardInput.WriteLine("D:");就无效了。删掉两条中的一条Cmd程序闪一下就自动关闭了。
请高手指教是怎么回事?

解决方案 »

  1.   

    参考:
    http://www.haoxiai.net/wangzhanzhizuo/aspnet/53727.html
      

  2.   

    Process process = new Process();
      process.StartInfo.FileName = "cmd.exe";
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.RedirectStandardInput = true;
      process.StartInfo.RedirectStandardOutput = true;
      process.StartInfo.RedirectStandardError = true;
      process.StartInfo.CreateNoWindow = true;  process.Start();
      process.StandardInput.WriteLine("");
      process.StandardInput.WriteLine("");
      process.StandardInput.WriteLine("");
      process.StandardInput.WriteLine("exit");
      process.Close();
      

  3.   

    3楼的还是得不到我想要的结果,我不希望使用process.StartInfo.CreateNoWindow = true;因为我在后面的process.StandardInput.WriteLine("");里调用某个程序,我希望在cmd窗口中看到程序运行的结果。我使用除了process.StartInfo.CreateNoWindow = true;这句以外你的全部代码,结果打开的cmd窗口还是一片空白,没有任何显示也不能输入任何字符,只能关闭。
      

  4.   

    你打开CMD干什么?是不是要执行DOS命令?如果是这样,建议不用打开CMD,直接将DOS命令(比如说Ping命令)写在程序中,用一个RichTextBox接收执行信息。我曾经也想向你这样打开CMD,然后再在里面敲入命令,搞了很久没有搞成功,和你现在的情况一样。以下为执行一个批处理("系统大扫除.bat")的源代码(其中调试的痕迹还在,没有删):
     private void ViewInfotbn_Click(object sender, EventArgs e)
            {
                this.ViewInfotbn.Enabled = false;
                /*
                   ProcessStartInfo start = new ProcessStartInfo("cmd.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
                   //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
                   //start.Arguments = "192.168.0.179";//设置命令参数
                   start.CreateNoWindow = true;//不显示dos命令行窗口
                   start.RedirectStandardOutput = true;//
                   start.RedirectStandardInput = true;//
                   start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
                   Process p = Process.Start(start);
                   StreamReader reader = p.StandardOutput;//截取输出流
                   string line = reader.ReadLine();//每次读取一行
                   while (!reader.EndOfStream)
                   {
                       this.ViewInfoRTB.AppendText(line + "\n\n");
                       line = reader.ReadLine();
                   }
                   p.WaitForExit();//等待程序执行完退出进程
                   p.Close();//关闭进程
                   reader.Close();//关闭流*/
                Process p = new Process();
                p.StartInfo.FileName = "系统大扫除.bat";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;//true表示不显示黑框,false表示显示dos界面              p.Start();
                // p.StandardInput.WriteLine("系统大扫除.bat");
                //p.StandardInput.WriteLine("exit");
               
              
                StreamReader reader = p.StandardOutput;
                string line = reader.ReadLine();//每次读取一行
                  while (!reader.EndOfStream)
                {
                    this.ViewInfoRTB.AppendText(line + "\n");
                    //this.ViewInfoRTB.AppendText("\n");                line = reader.ReadLine();
                }
                p.WaitForExit();
                p.Close();
                reader.Close();
                this.ViewInfoRTB.AppendText("系统垃圾文件已清理完毕!");
            }以上源代码是在如下项目中经过检验了的,你可以下载体会一下。
    【如果我的回答对你有所帮助,请到下列地址http://download.csdn.net/source/2247484 下载原创家庭记账软件,并给出宝贵意见!】
      

  5.   

    Process process = new Process();
      process.StartInfo.FileName = "cmd.exe";
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.RedirectStandardInput = true;
      process.StartInfo.RedirectStandardOutput = true;
      process.StartInfo.RedirectStandardError = true;
      process.StartInfo.CreateNoWindow = true;  process.Start();
      process.StandardInput.WriteLine("");
      process.StandardInput.WriteLine("");
      process.StandardInput.WriteLine("");
      process.StandardInput.WriteLine("exit");
      process.Close();
    就是这个啊 
      

  6.   

    很简单的
    一句代码搞定
    System.Diagnostics.Process.Start("cmd");