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();
11
  • can you point out which commands get ignored? Commented May 13, 2012 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... Commented May 13, 2012 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. msdn.microsoft.com/en-us/library/sxf2saat.aspx Commented May 13, 2012 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. Commented May 13, 2012 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. Commented May 13, 2012 at 19:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hans, there are absolutely no errors, also paths are absolute. Also please see additions above.
You broke my crystal ball. Great, I wasn't planning to go shopping today.
Sorry, don't get it, English is not my first language.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.