I want to make a Windows service that will monitor a path for a file. When one appears, it reads it and runs the program in the file. The structure of the command files is delay;path;filename;arguments. When testing this as a winforms app, it works great. But when I moved it to a service, it dies saying it cannot find the file.
I tried changing the service to run as my own AD account. I also tried adding code to log to the eventlog whether or not it can see the command file and the executable the command file should be running. While I know it's a terrible idea for production, just to test I set all related directories and files to have everyone:f NTFS permissions, but to no avail.
void onCreation(object sender, FileSystemEventArgs e) {
try {
Thread.Sleep(100);
if (File.Exists(e.FullPath)) {
log("Located cmd file [" + e.FullPath + "]", EventLogEntryType.Information);
} else {
log("Couldn't find cmd file [" + e.FullPath + "]", EventLogEntryType.Information);
}
string[] cmd = File.ReadAllText(e.FullPath).Split(';');
Process proc = new Process();
proc.StartInfo.WorkingDirectory = cmd[1];
proc.StartInfo.FileName = cmd[2];
proc.StartInfo.Arguments = cmd[3];
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
Dictionary<string, object> processData = new Dictionary<string, object>(){{"delay",cmd[0]},{"proc", proc},{"file",e.FullPath}};
Thread tmp = new Thread(new ParameterizedThreadStart(processThread));
tmp.Start(processData);
} catch (Exception ex) {
log("Error during onCreation: " + ex.Message, EventLogEntryType.Error);
}
}
void processThread(object procdata) {
Dictionary<string, object> processData = (Dictionary<string, object>)procdata;
try {
int delay = System.Convert.ToInt16(processData["delay"]);
Thread.Sleep(delay);
Process p = (Process)processData["proc"];
if (!File.Exists(p.StartInfo.WorkingDirectory + "\\" + p.StartInfo.FileName)) {
log("Couldn't find proc file [" + p.StartInfo.WorkingDirectory + "\\" + p.StartInfo.FileName + "]", EventLogEntryType.Information);
} else {
log("Located proc file [" + p.StartInfo.WorkingDirectory + "\\" + p.StartInfo.FileName + "]", EventLogEntryType.Information);
}
p.Start();
try {
File.Delete(processData["file"].ToString());
} catch (Exception ex) {
log("** Exception clearing file [" + processData["file"].ToString() + "]: " + ex.Message, EventLogEntryType.Warning, true);
}
} catch (Exception ex) {
log("Error during processThread: " + ex.Message, EventLogEntryType.Error);
log("Process data: [" + processData["delay"].ToString() + "][" + processData["file"].ToString() + "][" + ((Process)processData["proc"]).StartInfo.WorkingDirectory + "][" + ((Process)processData["proc"]).StartInfo.FileName + "][" + ((Process)processData["proc"]).StartInfo.Arguments + "]", EventLogEntryType.Information);
}
}
I have my config setting the listenpath to d:\misc\sites----.com\cmds then I say "Alexa, trigger fan test" and my web server generates a file in that folder like: 0;d:\misc\iot;wemo.exe;10.0.0.72 on; for it to process. It sees it and I get the following eventlog entries:
Located cmd file [d:\sites\----\www\cmds\15659086948907.dat]
Located proc file [d:\misc\iot\wemo.exe]
Error during processThread: The system cannot find the file specified
Process data: [d:\sites\----\www\cmds\15659086948907.dat][d:\misc\iot][wemo.exe][10.0.0.72 off]
What is it about being in a service that is making it fail? Thanks!