How can I have a .NET windows service execute a batch file?
Posted on 2010-08-26
I have a C# .NET Windows service in which I define a FileSystemWatcher component. This filewatcher watches a folder on my local machine and when a file gets created in this folder, on the create event, I start a process that will need to run a batch file (which picks up the file and posts it onto a tumbleweed ftp using the tumbleweed secure transport command line).
Here's the code in the created event of the FileSystemWatcher:
System.Diagnostics.ProcessStartInfo procStartInfo =
procStartInfo.FileName = "cmd.exe";
procStartInfo.Arguments = @"/c C:\temp\MyBat_TEMP.bat";
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
// Now create a process, assign its ProcessStartInfo and start it
//Declare and instantiate a new process component.
process1 = new System.Diagnostics.Process();
process1.StartInfo = procStartInfo;
catch (Exception ex)
However, when I start the windows service and put a new file in the watched folder, it does not execute the batch file, it basically does nothing, does not throw any errors. I copied this same piece of code into a console app and run it and it works fine (i.e. batch file commands are executed).
Is there an issue that anyone knows about, with executing batch files from a windows service? Also, I setup the service to run as "Local System".
Any help is appreciated. Thanks.