We help IT Professionals succeed at work.

How to execute .bat from C# service?

Hello I have a C# service that I have created.  Inside the .exe after doing certain things I need to be able to execute a .bat file.  
I don't have to worry about Input, Output or anything.

Currently I'm using the following and its not working.
Comment
Watch Question

BRONZE EXPERT

Commented:
try this

http://stackoverflow.com/questions/361097/c-sharp-service-cannot-execute-batch-file

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

Author

Commented:
nope didn't work.
BRONZE EXPERT

Commented:
Maybe you could duplicate the actions of the .bat with C#

Author

Commented:
no because the .bat executes a literall program.
Aaron JabamaniTechnical Architect

Commented:
Can we see your code ur trying to execute the batch file from the service ? Also the error / other details will be good.

Are you running the service with sufficient privileges to run a .bat file insider?
Awarded 2011
Awarded 2011
Commented:
Don't know about service.
From the .exe file created from this code in C#
it starts test.bat without problems and even daterts editor inside
the test.bat and opens window with the list.txt file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RunBatchFile
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "c:\\c_temp\\test.bat";
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;

            proc.Start();

            proc.WaitForExit
                (
                    500
                );

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

           string outputMessage = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit();

        }
    }
}

Open in new window


contents of the test.bat file:
dir >> c:\c_temp\list.txt
edit c:\c_temp\list.txt

Open in new window

Awarded 2011
Awarded 2011
Commented:
meant to write:

it starts test.bat without problems and even starts editor inside
the test.bat and opens window with the list.txt file



Aaron JabamaniTechnical Architect
Commented:
Looks like the windows service is not having permission. What OS you are working ? from windows vista you will get permission problems. From windows vista, if you trigger any exe from any application, you will see a "confirmatio alert" kinds.

Commented:
What's in the bat file?  Does it just launch another application?  And if so, why not launch that application directly rather than launching the bat file?