Solved

Execute batch file using windows service

Posted on 2013-01-15
3
1,762 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
Comment Utility
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
Comment Utility
I tried with all account still no luck. Even I didn't get error....
0
 
LVL 17

Author Closing Comment

by:sonawanekiran
Comment Utility
Your solution doesn't help...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

771 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

10 Experts available now in Live!

Get 1:1 Help Now