我从C#代码启动.bat文件:

     void ExecuteCommand(string command)
     {
        var processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;

        var process = Process.Start(processInfo);

        process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
            Console.WriteLine("output>>" + e.Data);
        process.BeginOutputReadLine();

        process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
            Console.WriteLine("error>>" + e.Data);
        process.BeginErrorReadLine();

        process.WaitForExit();

        Console.WriteLine("ExitCode: {0}", process.ExitCode);
        process.Close();
    }


它开始了,一切都执行了,但是:

set mydir = "C:\mydir"
xcopy /c /y "myFile.exe" %mydir%


该文件未复制

但是,当我从资源管理器运行相同的批处理文件时,该文件将被复制。有什么想法吗?

最佳答案

添加processInfo.RedirectStandardInput = true;为我工作-现在文件复制没有问题

10-08 04:57