Solved

Execute batch file using windows service

Posted on 2013-01-15
3
1,790 Views
Last Modified: 2013-01-16
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

0
Comment
Question by:sonawanekiran
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Jaroslav Mraz earned 500 total points
ID: 38778028
Hi,

it looks like the problem is your account :) Network services account have minimum rights to local computer. Cant you make normla accoun just for the service or you your onw to test.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms684272(v=vs.85).aspx
0
 
LVL 17

Author Comment

by:sonawanekiran
ID: 38781738
I tried with all account still no luck. Even I didn't get error....
0
 
LVL 17

Author Closing Comment

by:sonawanekiran
ID: 38785684
Your solution doesn't help...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now