edek k
edek k

Reputation: 103

Xcopy does not copy files when run from c#

I start the .bat file from c# code:

     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();
    }

It starts, everything gets executed, but the:

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

the file is not copied

But when I run the same batch file from explorer, the file gets copied. Any thoughts?

Upvotes: 3

Views: 3006

Answers (1)

edek k
edek k

Reputation: 103

Adding processInfo.RedirectStandardInput = true; worked for me - now files are copied without issues

Upvotes: 5

Related Questions