以下函数是用来在c#中调用“建目录tese1988”DOS命令,编译通过,但只弹出DOS黑框,却并没有建目录,问题出在什么地方呢? public static void ExeCommand()
        {
            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 = false;//true表示不显示黑框,false表示显示dos界面
           string strOutput = null;
            try
            {
                p.Start();
                p.StandardInput.WriteLine("mkdir test1988");
                p.StandardInput.WriteLine("exit");
               
                p.Close();
            }
            catch (Exception)
            {            }        }
      

解决方案 »

  1.   

    为什么简单问题复杂化?            try
                {
                    if (!Directory.Exists("test1988"))
                    {
                        Directory.CreateDirectory("test1988");
                    }
                }
                catch { }不好吗?
      

  2.   

    控制台模式下 建的目录在 bin\Debug 下面.D:\testcon\testcon\bin\Debug\test1988
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;namespace testcon
    {
        class Program
        {
            static void Main(string[] args)
            {
                ExeCommand();
            }        public static void ExeCommand()
            {
                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 = false;//true表示不显示黑框,false表示显示dos界面 
                string strOutput = null;
                try
                {
                    p.Start();
                    p.StandardInput.WriteLine("mkdir test1988");
                    p.StandardInput.WriteLine("exit");                p.Close();
                }
                catch (Exception)
                {            }        }     }
    }
      

  3.   

    可以用绝对路径:p.StandardInput.WriteLine(@"mkdir c:\test1988");
    p.StandardInput.WriteLine("exit");这样新目录 c:\test1988
      

  4.   

    如果不是练习DOS命令调用, 实际对目录操作,直接使用.NET类库using System.IO;Directory.CreateDirectory(路径);
    Directory.Delete(路径);
    Directory.Move(源路径,目标路径);
      

  5.   

    C#中运行DOS命令方法一:调用cmd.exestring command = "ping google.com -n 15"; //这里添加你的命令
    System.Diagnostics.Process.Start("cmd", "/c" + command);方法二:把DOS命令写成一个bat批处理,直接用Process.Start运行批处理文件
      

  6.   


    正像8L说的,只是为了练习C#中运行DOS命令,并非真的为了建目录。我已试过了,楼上都是正解。现在有个问题,我真实的想法是想调用批处理,确实批处理也调用成功了,但我现在想把调用批处理的过程中的提示显示在控制台的黑屏中,让用户知道确实批处理调用了,目前的状况是控制台的黑屏中没有任何提示。我修改了程序如下:   public static string ExeCommand(string commandText)
            {
                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 = false;//true表示不显示黑框,false表示显示dos界面
                string strOutput = null;
                try
                {
                    p.Start();
                    p.StandardInput.WriteLine(commandText);
                    //p.StandardInput.WriteLine("exit");
                    strOutput = p.StandardOutput.ReadToEnd();
                    p.WaitForExit();
                    p.Close();
                }
                catch (Exception e)
                {
                    strOutput = e.Message; 
                }
                return strOutput; 
            }
            private void 系统大扫除ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                string dosCommand = "系统大扫除.bat";
                string strRst = ExeCommand(dosCommand);
                Console.WriteLine(strRst);
                Console.ReadLine();
            }
    但控制台的黑屏中仍然没有任何提示。
      

  7.   

    不用回车,你要看下DOS窗口里的提示是什么?执行完指定命令后的当前目前在哪?另,参考下DOS的帮助:
    C:\Documents and Settings\Administrator>cmd/?
    启动 Windows XP 命令解释程序一个新的实例CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:
        [[/S] [/C | /K] string]/C      执行字符串指定的命令然后终断
    /K      执行字符串指定的命令但保留
    /S      在 /C 或 /K 后修改字符串处理(见下)
    /Q      关闭回应
    /D      从注册表中停用执行 AutoRun 命令(见下)
    /A      使向内部管道或文件命令的输出成为 ANSI
    /U      使向内部管道或文件命令的输出成为 Unicode
    /T:fg   设置前景/背景颜色(详细信息,请见 COLOR /?)
    /E:ON   启用命令扩展(见下)
    /E:OFF  停用命令扩展(见下)
    /F:ON   启用文件和目录名称完成字符 (见下)
    /F:OFF  停用文件和目录名称完成字符(见下)
    /V:ON   将 ! 作为定界符启动延缓环境变量扩展。如: /V:ON 会
            允许 !var! 在执行时允许 !var! 扩展变量 var。var 语法
            在输入时扩展变量,这与在一个 FOR 循环内不同。
    /V:OFF  停用延缓的环境扩展。
      

  8.   

    p.StartInfo.RedirectStandardInput = true;从流读取,不显示
      

  9.   


    能显示,至少结合WINAPI可以实现,你自己试吧。我对这种实现方式实在是不感兴趣了。能用windows实现的,就尽量避开用DOS。从DOS --> 控制台 --> WINFORM --> WPF  技术发展那么快。人的时间是有限的,要学习最有用的知识。
      

  10.   

    C#中运行命令行截取输出流的例子
    说明:经常有朋友问如何在C#中运行一个dos命令,并截取输出、输出流的问题,这个问题我以前在Java中实现过,由于在C#中没有遇到过类似的情况,为了避免每次别人问都要一遍一遍演示的情况,特地做了一个简单的例子,实现在WinForm中ping一个网站,并且将ping的结果显示在一个文本框中。有图有真相: private void btnExecute_Click(object sender, EventArgs e)
            {
                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();//关闭流
            }
      

  11.   

    一起学习学习。DOS的问题,平时很少遇到。
      

  12.   


    txtCommand是什么啊?错误 1 当前上下文中不存在名称“txtCommand” E:\c#\gold\gold3\ClearSystem.cs 29 31 gold
      

  13.   

    周公的方法:是不用DOS输入输出窗口, 取得输出结果显示到 ListBox(或TextBox)中。txtCommand 是个textBox控件,用来输入 start.Arguments 参数. 即图中的“www.163.com”
      

  14.   


    如果想在DOS窗口显示输入输出,试试
    [DllImport("user32.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);// 查找DOS窗口
    IntPtr hwnd= FindWindow ("ConsoleWindowClass", null); Sendkey.send(...)