Hi,
This problem has been frustrating me for days and I hope you experts can
help me out. I am trying to run a command, which I would normally run from
the command line, from within my C# application. The command runs
successfully from a Windows Form but fails if the application is recasted as
a Windows service. The application is essentially a File Watcher that should
run the process when a file is dropped into a specific directory.
Here is the code:
private void FileMonitor_Changed(object sender,
System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
string renamed = e.FullPath;
System.Diagnostics.Process process1;
process1= new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;
string strCmdLine;
strCmdLine = "/K Dumper.exe \"" + renamed + "\"";
Thread.Sleep(4000);
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
process1.StartInfo.EnvironmentVariables.Clear();
foreach (DictionaryEntry de in environmentVariables)
{
// EventLog.WriteEntry("Directory Monitor", " KeyValue: " + de.Key +
de.Value);
process1.StartInfo.EnvironmentVariables.Add((strin g)de.Key,
(string)de.Value);
}
process1.StartInfo.WorkingDirectory = "C:\\Temp";
process1.StartInfo.FileName = "CMD.exe";
process1.StartInfo.Arguments = strCmdLine;
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.RedirectStandardError = true;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.CreateNoWindow = true;
process1.Start();
StreamReader myStreamReader = process1.StandardOutput;
StreamReader myStreamError = process1.StandardError;
string myID = myStreamReader.ReadLine();
string stdError = myStreamError.ReadLine();
process1.Close();
EventLog.WriteEntry("Directory Monitor", "myID: " + myID);
EventLog.WriteEntry("Directory Monitor", "stdError: " + stdError);
}
The event log reports the following error:
stdError: 'Dumper.exe' is not recognized as an internal or external command
Configuring the service so that it can interact with the desktop doesn't
help.
Any ideas why my application can't find the command? Perhaps I have not set
up the environment variables correctly?
Many thanks in advance and happy holidays!