There is such a script

@echo off xcopy /R /Y "D:\test.dll" "C:\Users\testuser\Desktop" 

If you run it, it correctly processes and copies the file. If you run it from this code, the file for some reason is not copied! But the most interesting fact is that if I disable RedirectStandardOutput then the file is copied. What is the problem?

 var processInfo = new ProcessStartInfo("cmd.exe", "/c " + @"D:\test.bat"); processInfo.WorkingDirectory = windir; processInfo.CreateNoWindow = true; processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.StandardOutputEncoding = Encoding.GetEncoding(866); var process = Process.Start(processInfo); process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => { if (!string.IsNullOrWhiteSpace(e.Data)) { LogTextEvent(rtbLog, Color.Black, e.Data); } }; process.BeginOutputReadLine(); process.WaitForExit(); process.Close(); 
  • So you have a problem with the redirect. Try a different way to redirect. For example, msdn msdn.microsoft.com/ru-ru/library/… or dotnetperls.com/redirectstandardoutput - nick_n_a
  • Tried, does not help. - Dmitriy
  • Reading StdOutput in your own thread? - nick_n_a
  • Yes, the process starts in a separate thread - Dmitriy
  • one
    I will clarify once again. You should have three threads. One is the main one (is), the second is the start of the batch file (is), and the third one that reads stdout is probably better one byte. bat-program may not give \r\n (no). - nick_n_a

2 answers 2

I sent you examples in the links. They work, here's the first link (correct the directories for your own)

 using System; using System.Xml; using System.IO; using System.Text; using System.Diagnostics; public class Demo { static void Main() { ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/c " + @"test.bat"); processInfo.WorkingDirectory = "d:\\work\\cs"; processInfo.CreateNoWindow = true; processInfo.UseShellExecute = false; processInfo.RedirectStandardOutput = true; processInfo.StandardOutputEncoding = Encoding.GetEncoding(866); // var process = Process.Start(processInfo); using (Process process = Process.Start(processInfo)) { // // Read in all the text from the process with the StreamReader. // using (StreamReader reader = process.StandardOutput) { string result = reader.ReadToEnd(); Console.Write(result); } } }} 

Result

 D:\work\cs>1.exe D:1.exe Скопировано файлов: 1. 

This answer is really not the best, for good it is necessary to intercept both Stdoutput and Stderror and Stdinput, but as a special case it will do.

  • Hm Strange fact. If you run your example in the console application, it runs, the file is copied. But he is on the WinForm button, the file is not copied. But if you turn off the RedirectStandardOutput file is copied. I do not understand how this is connected, because this is just a conclusion and not a performance. - Dmitriy

It turns out it was in the xcopy team. It is in WinForm that is important! Both commands work in the console application.

Does not work:

 xcopy /R /Y "D:\test.dll" "C:\Users\testuser\Desktop" 

Works:

 copy /Y "D:\test.dll" "C:\Users\testuser\Desktop" 
  • Strange, I put cs-ku with the csc.exe 1.cs /target:winexe parameter csc.exe 1.cs /target:winexe removed Console.Write - and it worked. - nick_n_a