2

i'm trying to execute a .bat file through a c# console application using code from here: Service hangs up at WaitForExit after calling batch file

Kevin's solution kinda works, but some commands in my .bat file get ignored for some reason, but when i manually execute the .bat file all commands work just fine.

e.g. xcopy command doesn't work while executing .bat from console app, but start command works fine.

Any idea why this happens?

p.s. recently i found that if the program is being launched from command prompt, it works well. How come? Still i need to put it on autorun, so this doesn't solve the problem.

Also, if launched by clicking on exe file, output shows

xcopy folder1 folder2

but if launched from command prompt, output shows

xcopy folder1 folder2

smth/smth.smth copied

....

5 files copied.

And it actually is being copied.

proc.StartInfo.FileName                 = target;
        proc.StartInfo.RedirectStandardError    = true;
        proc.StartInfo.RedirectStandardOutput   = true;
        proc.StartInfo.UseShellExecute          = false;

        proc.Start();

        proc.WaitForExit
            (
                (timeout <= 0)
                ? int.MaxValue : timeout * NO_MILLISECONDS_IN_A_SECOND *
                   NO_SECONDS_IN_A_MINUTE
            );

        errorMessage    = proc.StandardError.ReadToEnd();
        proc.WaitForExit();

        outputMessage   = proc.StandardOutput.ReadToEnd();
        proc.WaitForExit();
Community
  • 1
  • 1
athe
  • 21
  • 3
  • can you point out which commands get ignored? – Shai May 13 '12 at 13:30
  • 3
    Did you set `WorkingDir` inside your process before running it? If you did not your batch file could not find files defined with partial path... – Marco May 13 '12 at 13:32
  • Maybe it's a security issue (the spawned process doesn't have write access to some of your files/paths). An overload of the `Start()` method lets you provide a username/password. http://msdn.microsoft.com/en-us/library/sxf2saat.aspx – Chris Sinclair May 13 '12 at 13:33
  • 3
    You can get console standard output and error output redirected to you program. Maybe xcopy prints an error that might help. – Sascha May 13 '12 at 13:38
  • Marco, i'm using absolute paths, so that shouldn't be the problem. Sascha, yes I looked at that, absolutely no errors, output shows that all commands are executed, but they are not. Chris, no password is used on ANY user, and the programm is run "as administrator" in windows 7. – athe May 13 '12 at 19:39
  • Shai, rmdir and xcopy both don't work. – athe May 13 '12 at 19:43
  • Put a deliberate error in your batch file, e.g., xxcopy instead of xcopy, and see whether you see the error message or not. – Harry Johnston May 13 '12 at 23:24
  • Yep, i get Error message: 'xxcopy' is not recognized as an internal or external command, operable program or batch file. – athe May 14 '12 at 09:38
  • Try replace xcopy with cmd /c xcopy and see what happens. – Harry Johnston May 14 '12 at 21:23
  • There are a number of different code fragments in the question you reference. Could you put the actual code you're using in your question? – Harry Johnston May 14 '12 at 21:24
  • I can't reproduce the problem you're describing, but I think you need to use asynchronous IO. The code you've given will deadlock if there is more standard output than will fit in the buffer. – Harry Johnston May 15 '12 at 22:32

1 Answers1

2

Batch file commands do not get ignored when you execute the command processor from a service. They however easily fail and can do so unobserved since you cannot see their output. Typical reasons for failure are having the ProcessStartInfo.WorkingDirectory not set correctly so that relative paths no longer work or trouble caused by the service running with a different user account that doesn't have the same rights as the one you use from the desktop.

Diagnose problems by redirecting output to a file, run cmd.exe /c and use the > operator. Append 2>&1 so that the file contains both regular and error output. And by using the %errorlevel% variable and EXIT command judiciously so you can discover that execution failed by using the Process.ExitCode property.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Hans, there are absolutely no errors, also paths are absolute. Also please see additions above. – athe May 13 '12 at 19:53
  • You broke my crystal ball. Great, I wasn't planning to go shopping today. – Hans Passant May 13 '12 at 19:55
  • Sorry, don't get it, English is not my first language. – athe May 13 '12 at 20:02
  • As long as you don't tell us *exactly* what commands you use and *exactly* how you know they didn't do the job then I have no good guess left what your problem might be. – Hans Passant May 13 '12 at 20:55
  • As i mentioned in comments above, i'm using rmdir and xcopy. I can tell they don't work because they kinda don't work i.e. they don't delete or copy anything. although they work if program is being launched from command prompt. – athe May 13 '12 at 21:49
  • Great, got that new crystal ball and you broke it *again*. It couldn't cope with "kinda". Glowed red of a minute and poof! Signing off, got some careful recycling to do so I don't spend the whole freaking' week at the shopping mawl. – Hans Passant May 14 '12 at 00:11