日期:2014-05-18  浏览次数:20773 次

C#执行DOS命令错误,请帮忙看看,谢谢!
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命令我试过了,没有问题,可是在这段代码中就是不行,没有反映,我找了很长时间都没找到问题,请大家指教

------解决方案--------------------
参考
http://blog.csdn.net/jinjazz/archive/2008/05/07/2413039.aspx

C# code
using 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();
        }
    }
}

------解决方案--------------------
完成这个工作的时间比较长,主线程就一直在进行处理,所以没有反应,程序像死了
你可以自己创建个线程进行处理
------解决方案--------------------
加上这句Application.DoEvents();,像窗体代码继续运行,调用DOS让他自己去执行去
------解决方案--------------------
c#中运行命令行截取输出流的例子
说明:经常有朋友问如何在c#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在java中实现过,由于在c#中没有遇到过类似的 情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在winform中ping一个网站,并且将ping的结果显示在一个