troubleshooting Question

Windows Service does not work but Exe with UI works fine

Avatar of zakali
zakali asked on
C#
7 Comments2 Solutions287 ViewsLast Modified:
We have a simple Windows Service that we wrote in C# that is supposed to watch a folder and run a batch file when a new file arrives. On two systems (Win XP and Win 2008), the service works fine but on a Windows 2003 server, it does not work. We made an EXE with UI and tested and that works fine. The service shows up under services.msc and you can start and stop it. There are no errors in Event Log but it does not run that batch file.
Any ideas?

I have pasted the code here as well.
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
RunBatchFile 
{
public partial class RunBatchFile : ServiceBase 
{
public RunBatchFile() 
{
InitializeComponent();
RunBatch();
}
protected override void OnStart(string[] args) 
{
}
protected override void OnStop() 
{
}
public void RunBatch() 
{
String fileName = "c://test.bat"; 
String logfileName = "c://test.txt"; 
String EntireFile; 
string path = @"c:\\test\\RunBatchInfo.txt"; 
try 
{
StreamReader sr = new StreamReader(path); 
EntireFile = sr.ReadToEnd();
char[] token = new char[] { System.Environment.NewLine.ToCharArray()[0] }; 
sr.Close();
string[] sWatcfolderinto = EntireFile.Split(token); 
fileName = sWatcfolderinto[0].Replace(
"\n", "1"); 
logfileName = sWatcfolderinto[1].Replace(
"\n", ""); 
String WatchFolderName = sWatcfolderinto[2].Replace("\n", ""); 
fileSystemWatcher1.Path = WatchFolderName;
System.Diagnostics.
Process proc = new System.Diagnostics.Process(); 
proc.StartInfo.FileName = fileName;
proc.StartInfo.RedirectStandardError = 
true; 
proc.StartInfo.RedirectStandardOutput = 
true; 
proc.StartInfo.UseShellExecute = 
false; 
proc.StartInfo.CreateNoWindow = 
true; 
proc.Start();
proc.WaitForExit();
String output1 = proc.StandardError.ReadToEnd(); 
proc.WaitForExit(); 
 
// HandledEventArgs 
 
String output2 = proc.StandardOutput.ReadToEnd(); 
proc.WaitForExit();
 
//output1 = proc.StandardError.ReadToEnd(); 
//TextWriter writer = new StreamWriter("D://Work//log//FolderLog.txt", true); 
 
TextWriter writer = new StreamWriter(logfileName, true); 
writer.WriteLine(
DateTime.Now + " " + output1 + " \n " + output2 + " has been renamed. "); 
writer.Close();
//proc.WaitForExit(); 
//writer = new StreamWriter("D://Work//log//FolderLog.txt", true); 
//writer.WriteLine(DateTime.Now + " A new folder/file with name " + proc.StandardError.ReadToEnd() + " has been renamed. "); 
//writer.Close(); 
}
catch (Exception e) 
{
TextWriter writer = new StreamWriter(logfileName, true); 
writer.WriteLine(
DateTime.Now + " Error occure " + e.Message); 
writer.Close();
}
}
private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) 
{
RunBatch();
}
}
}
ASKER CERTIFIED SOLUTION
jacekmalinowski

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros