Link to home
Create AccountLog in
Avatar of zakali
zakali

asked on

Windows Service does not work but Exe with UI works fine

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();
}
}
}

Open in new window

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
It's possible that on Windows 2003/2008 your server has no rights to write/modify files so it can not do your task. In this case the server has no rights to write in the log file as well. You can run filemon ( http://technet.microsoft.com/en-us/sysinternals/bb896642.aspx ) to monitor files activity at your server. This program will show information if your server tries to open/change files and display result.

I hope that will help.
Avatar of zakali
zakali

ASKER

I stopped the service and changed the account to administrator but that did not help. I'll try to monitor files activity using filemon. Thanks
When you say "it doesn't run," what do you mean? Do you mean you don't see it run, or that it doesn't move files. Because you executing a process from within the code space of  a service, you will not see any interaction with the desktop of the logged in user--they are two separate spaces.
Avatar of zakali

ASKER

I mean it does not run the batch file and it does not make an entry in the log file. The service seems to be running. I ran Process Monitoring tool but I am not sure what I am supposed to be looking for.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
BTW. You could change String fileName = "c://test.bat"  to "c:\\test.bat"

"\\" gives \
but
"//" gives //