System.Diagnostics.Process pro = new Process();
             expstring = "cmd.exe";
             pro.StartInfo.FileName = expstring;
             pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
             pro.StartInfo.UseShellExecute = false;
             pro.StartInfo.RedirectStandardInput = true;
             pro.StartInfo.RedirectStandardOutput = true;
             pro.Start();
             pro.StandardInput.WriteLine("exp abc/abc@zgworcl buffer=10000 file=c:\aa.dmp owner=abc");
             pro.StandardInput.WriteLine("exit");
             string output = pro.StandardOutput.ReadToEnd();
             textBoxOutput.Text = output;
DOS命令我试过了,没有问题,可是在这段代码中就是不行,没有反映,我找了很长时间都没找到问题,请大家指教

解决方案 »

  1.   

    .....现在又没有问题了,不过问题先放这,比如我想显示出来DOS执行过程中的信息,有什么比较好的方法。还有这段代码感觉很怪,程序给人感觉都死在那了,所以刚才我以为不行,大家有什么好的想法指教一下,谢谢!
      

  2.   

    参考
    http://blog.csdn.net/jinjazz/archive/2008/05/07/2413039.aspxusing System;
    using System.Windows.Forms;namespace WindowsApplication8
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        delegate void dReadLine(string strLine);
            private void excuteCommand(string strFile, string args, dReadLine onReadLine)
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo = new System.Diagnostics.ProcessStartInfo();
                p.StartInfo.FileName = strFile;
                p.StartInfo.Arguments = args;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.Start();
                System.IO.StreamReader reader = p.StandardOutput;//截取输出流
                string line = reader.ReadLine();//每次读取一行
                while (!reader.EndOfStream)
                {
                    onReadLine(line);
                    line = reader.ReadLine();
                }
                p.WaitForExit();
            }        private void button1_Click(object sender, EventArgs e)
            {
                excuteCommand("ipconfig", "", new dReadLine(PrintMessage));
            }
            private void PrintMessage(string strLine)
            {
                this.textBox1.Text += strLine + " ";
                Application.DoEvents();
            }
        }
    }
      

  3.   

    加上这句Application.DoEvents();,像窗体代码继续运行,调用DOS让他自己去执行去
      

  4.   

    c#中运行命令行截取输出流的例子
    说明:经常有朋友问如何在c#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在java中实现过,由于在c#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在winform中ping一个网站,并且将ping的结果显示在一个
      

  5.   

    tbResult.Text = "";
                ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
                //如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
                start.Arguments = txtCommand.Text;//设置命令参数
                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)
                {
                    tbResult.AppendText(line+" ");
                    line = reader.ReadLine();
                }
                p.WaitForExit();//等待程序执行完退出进程
                p.Close();//关闭进程
                reader.Close();//关闭流
            }
      

  6.   

    System.Diagnostics.Process pro = new Process();
                        expstring = "cmd.exe";
                        pro.StartInfo.FileName = expstring;
                        pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                        pro.StartInfo.CreateNoWindow = true;
                        pro.StartInfo.UseShellExecute = false;
                        pro.StartInfo.RedirectStandardInput = true;
                        pro.StartInfo.RedirectStandardOutput = true;
                        pro.Start();
                        pro.StandardInput.WriteLine(@"exp abc/abc@zgworcl buffer=10000 file=c:\aa.dmp owner=abc");
                        Application.DoEvents();
                        StreamReader reader = pro.StandardOutput;
                        string line = reader.ReadLine();
                        while (!reader.EndOfStream)
                        {
                            PrintMessage(line);
                            line = reader.ReadLine();
                        }
                        pro.StandardInput.WriteLine(@"exit");
                        pro.WaitForExit();
                        pro.Close();
    CLR  无法从  COM  上下文  0x1b1c38  转换为  COM  上下文  0x1b1da8,这种状态已持续  60  秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送  Windows  消息的情况下处理一个运行时间非常长的操作。这种情况通常会影响到性能,甚至可能导致应用程序不响应或者使用的内存随时间不断累积。要避免此问题,所有单线程单元(STA)线程都应使用泵式等待基元(如  CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。 
    出现这个异常。
    我的本意是向dos那样在textbox中显示DOS的输出流,学着各位前辈仿写了一下代码。请问这样怎么处理?开新线程,那textbox能否同时显示DOS的输出呢?
      

  7.   

    在Debug -> Exceptions -> Managed Debug Assistants里 去掉ContextSwitchDeadlock一项前面的钩。 
    在调试(菜单)-->异常-->-> Managed Debug Assistants里 去掉ContextSwitchDeadlock一项前面的钩。 
    http://www.cnblogs.com/shanyou/archive/2007/12/09/988488.html