Link to home
Start Free TrialLog in
Avatar of Kiran Sonawane
Kiran SonawaneFlag for India

asked on

Execute batch file using windows service

I tried almost first 2 pages of google search with text "calling batch file from windows service". Still no luck. Also tried to edit registry of service (my service) as suggested in one of the link. My service is running under "Network services" account

Here is my sample code

private static void InvokeBatchFile()
        {
            using (System.Diagnostics.Process process = new System.Diagnostics.Process())
            {
                process.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
                process.StartInfo = new System.Diagnostics.ProcessStartInfo(@"c:\windows\system32\cmd.exe");
                process.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");

               process.StartInfo.Arguments = string.Format(@"/C C:\data\test.bat");
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.ErrorDialog = false;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;

                process.Start();

             

            }
        }

Open in new window


Batch file code

echo "Batch file invoked" >> kiran.txt

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jaroslav Mraz
Jaroslav Mraz
Flag of Slovakia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kiran Sonawane

ASKER

I tried with all account still no luck. Even I didn't get error....
Your solution doesn't help...