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("****.bat"); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd(); 

解决方案 »

  1.   

    不行啊,大哥,你的这个就直接退出去了,我的意思是CMD窗口还要留着显示信息的啊。。
      

  2.   

    那你不要 p.StandardInput.WriteLine("exit"); 啊
      

  3.   

    "CMD窗口还要留着显示信息"你的意思是要显示CMD黑窗口?那就不要p.StartInfo.CreateNoWindow = true; 
      

  4.   

    如果要看到显示 comprocess.StartInfo.RedirectStandardOutput = true;  这个也不要
      

  5.   

    http://topic.csdn.net/u/20080321/17/8539843b-39ac-4119-b58f-a77375d4a046.html
      

  6.   

    首先你不想退出,那就要挂起该线程,如果是不是主线程可用循环+通知的方式,也可用循环锁+Sleep的方式.如果是主线程,当然就不能挂起了 可用事件进等对某个对象的等待完成而挂起,但这个就屏蔽了Windows的消息.
      

  7.   

    dll的实现:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace csdn_dll
    {
        public class Class1
        {
            public static string cmd = "";
            public ThreadStart ts1;
            public Thread t1;        public Class1()
            {
                ts1 = new ThreadStart(thread1);
                t1 = new Thread(ts1);
                //t1.IsBackground = true;
                t1.Name = "my thread";
                t1.Start();
            }        public void sendcmd(string mycmd)
            {
                cmd = mycmd;
            }        private static void thread1()
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();            p.StartInfo.FileName = "cmd.exe";            p.StartInfo.UseShellExecute = false;            p.StartInfo.RedirectStandardInput = true;            p.Start();            while (true)
                {
                    if (cmd != "")
                    {
                        p.StandardInput.WriteLine(cmd);
                        cmd = "";
                    }
                }
            }
        }
    }
    [code]试验console的代码[code=C#]
     static void Main(string[] args)
            {           
                string w ="dir";
                csdn_dll.Class1 cl = new csdn_dll.Class1();            while (cl.t1.IsAlive)
                {                if (w != "")
                    {
                        cl.sendcmd(w);
                        w = "";
                    }
                    
                }
                
            }
    具体使用的时候开个线程跑while,改全局w的值就可以了
      

  8.   

    The following article might be helpful:http://yz101.spaces.live.com/blog/cns!BC8AD37767C117E!166.entry