Running batch file from C #

UPDATE ** EVERYTHING VIEW EXCLUSIVE ANSWER ** I have the following code in my Windows service and I want to run a batch file. I want a command line window so that I can see the progress

here is my code but my batch file is not working

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.IO; namespace Watcher { public partial class Watcher : ServiceBase { public Watcher() { InitializeComponent(); FolderWatcher.Created += FolderWatcher_Created; FolderWatcher.Deleted += FolderWatcher_Deleted; FolderWatcher.Renamed += FolderWatcher_Renamed; } protected override void OnStart(string[] args) { // Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = "C:\\myFile.bat"; p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); } protected override void OnStop() { } private void FolderWatcher_Created(object sender, System.IO.FileSystemEventArgs e) { TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true); writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been created. "); writer.Close(); } private void FolderWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e) { TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true); writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been deleted. "); writer.Close(); } private void FolderWatcher_Renamed(object sender, System.IO.RenamedEventArgs e) { TextWriter writer = new StreamWriter("C:\\folder\\log.txt", true); writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been renamed. "); writer.Close(); } } } 

It does not execute a batch file. I'm new to .net and C #, and I'm not sure what to do next. thanks

+8
c # visual-studio-2010 windows-services
source share
6 answers

How to start a console application from a Windows service?

You want to set p.StartInfo with FileName = "cmd.exe" and Arguments = "c: \\ thebatfile.bat", I believe

+4
source share

The problem is that you have UseShellExecute as false, but you are not passing the name of the executable.

When ShellExecute used, it looks like double-clicking on a file in Explorer - it knows that .doc files must be opened with Word and that .bat files need to be opened with cmd.exe . However, when you turned off this function, it does not know any of this, and you need to transfer the executable file so that everything runs successfully.

When you set the value of RedirectStandardOutput to true, you need to run the batch file via cmd.exe instead, setting the FileName to cmd.exe and the /C "c:\myFile.bat" :

 p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/C \"c:\\myFile.bat\""; 
+2
source share

It looks like it starts the script package when the service starts first and then exits ( p.WaitForExit(); ) before other functions get the opportunity to call. Is this intended behavior? This explains why you can see that it performs operations on the folder and does not see the script being executed.

Try this code to open a console window. This should give you an idea of ​​starting a script package.

 protected override void OnStart(string[] args) { // Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; /* This is commented out so we can see what the script is doing inside the cmd console. */ //p.StartInfo.RedirectStandardOutput = true; p.StartInfo.FileName = "C:\\myFile.bat"; p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. /* Since we aren't redirecting the output, we have to comment out this line or we get an error */ //string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); } 
0
source share

I doubt your service or bat file. change the source code to open notepad! check if notepad appears! if so, then we can explore further!

0
source share

What does your batch file do? Suppose you confirm that it is working fine.

0
source share

Windows services start under a user account without work. To see the cmd window, you must impersonate the currently logged in user and run the cmd window on this user's desktop. See this:

Impersonating Windows with C #

0
source share

All Articles