我用C#调用dos,想输入多行命令的方法。但是往往只有第一行命令被执行。
触发命令如下:
        private void button3_Click(object sender, EventArgs e)
        {
            string m;
            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("del /f /s /q %systemdrive%\\*.tmp\rdel /f /s /q %systemdrive%\\*._mp\rdel /f /s /q %systemdrive%\\*.log\rdel /f /s /q %systemdrive%\\*.gid\rdel /f /s /q %systemdrive%\\*.chk\rdel /f /s /q %systemdrive%\\*.old\rdel /f /s /q %systemdrive%\\recycled\\*.*\rdel /f /s /q %windir%\\*.bak\rdel /f /s /q %windir%\\prefetch\\*.*\rrd /s /q %windir%\\temp & md %windir%\\temp\rdel /f /q %userprofile%\\recent\\*.*\rdel /f /s /q \"%userprofile%\\Local Settings\\Temporary Internet Files\\*.*\"\rdel /f /s /q \"%userprofile%\\Local Settings\\Temp\\*.*\"\rdel /f /s /q \"%userprofile%\\recent\\*.*\"");            p.StandardInput.WriteLine("exit");
        }
我也试过把要输入的命令赋值给n,但是一样不行。特来求救!!!HELP!!

解决方案 »

  1.   

    在p.start();后面加上p.Sleep(1000);试试。以前我也写过,由于貌似cmd响应速度问题,命令不成功。
    后来,我直接把批处理写在一个*.bat文件中,然后在用c#调用这个批处理,这样还省事。
      

  2.   


    我想只建立一个。exe文件。
      

  3.   

    前段时间写的一个,感觉和LZ的要求有点像!http://topic.csdn.net/u/20091017/01/14ecfe58-80f4-4d1c-87e1-14038f9ddd4b.html
      

  4.   

    写批处理,执行完后让bat自我删除掉就行了.这个最简单;
    或者...如果LZ熟悉匿名管道的话可以试试重定向cmd的IO,
    在Input的一端写入命令让cmd去执行.
      

  5.   

    这是我以前做的调用 Oracle 导入/导出命令的代码片段,供楼主参考:
    string backupcmd = string.Format(" exp {0}/{1}@{2} owner={0} file={3} compress=y", user, pwd, sid, outputfile);System.Diagnostics.Process backupprocess;
    backupprocess = System.Diagnostics.Process.Start("cmd.exe", "/c " + backupcmd);
    backupprocess.WaitForExit();        // 等待处部进程处理结束,同步方式
    int exitcode = backupprocess.ExitCode;
    backupprocess.Close();return exitcode == 0;