I have faced a case when the same batch file works differently from command line and when it is fired from a WCF service hosted on IIS. The difference is in XCOPY command. when I am running the batch file normally than XCOPY moves all data I need
XCOPY "C:\from" "C:\to" /K /R /E /I /S /C /H /G /X /Y
but when it runs from the WCF service nothing is copied. for running the batch from my service I am using the following code Executing Batch File in C# whith some little modifications. My Application pull is running under LocalSystem account. I also tryed to use my own account for the application poll - does not work. what is wrong?
Short update: What I have learned recently is that my WCF service is running under the App Pool User, but the process isn't. In purpose of experiment I have made an update in the process-start code
var pwdArray = "mypassword".ToArray();
var pwd = new System.Security.SecureString();
Array.ForEach(pwdArray, pwd.AppendChar);
processInfo.UserName = "myuser";
processInfo.Password = pwd;
processInfo.Domain = "LocalMachine";
but it does not help. Seems there is a mystic in running XCOPY under described conditions.
One more update: The same problem with XCopy is also found in a process that starts under a regular windows service.
Managed to solve my poblem thanks to this post (http://social.msdn.microsoft.com/Forums/vstudio/fr-FR/ab3c0cc7-83c2-4a86-9188-40588b7d1a52/processstart-of-xcopy-only-works-under-the-debugger?forum=netfxbcl) so actually the answer is:
This is a quirk of xcopy.exe. If you redirect the output, you have to redirect the input as well.
var command = "XCOPY ...."
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = true;
// *** Redirect the output ***
// unfortunately you cannot do it for X Copy
//processInfo.RedirectStandardError = true;
//processInfo.RedirectStandardOutput = true;
process = Process.Start(processInfo);
process.WaitForExit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With