0

I have a custom console command, which if executed in command prompt just displays some text in the command window (similar to 'dir' command).

I'm trying to execute the command 'myCommand' from a windows service and it is always returning blank string values. However, if run the same command in a command prompt, the result is displayed in the command window. Also, if I run the below code in a windows forms application, it works fine and I get the same data which is displayed in the command screen to a string variable.

Where am I going wrong? Why is the code returning a blank string when I execute it from a windows service, where as it works in a forms application. Is there any other method to capture the result of a command to a string variable in a windows service? Please advise. Thank you.

System.Diagnostics.ProcessStartInfo procStartInfo = 
    new System.Diagnostics.ProcessStartInfo("cmd", "/c myCommand");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • 1
    How do you know that your command works from a service? – Gabe Aug 15 '11 at 12:36
  • It is not working from a service. That is the problem. It works fine from a windows forms application. – Vinay Sathyanarayana Aug 15 '11 at 12:38
  • under which user is the windows services running ? what does the command do ? is it in the PATH ? Usually such things are related to permissions/rights... please give more information – Yahia Aug 15 '11 at 12:39
  • To make it more clear, let us say I have to run the 'dir' command in windows command console. If i execute dir in a command prompt, it will display the list of directories and files in the command window. Now, if I replace 'myCommand' the code i gave with 'dir' and execute it when I press a button in a windows forms application, I get the same result and it will be stored in the string variable - 'result'. But if i run the same code in a windows service, always the result will be "". Not sure why. Service is running under my ID. Please correct and advise wherever necessary. Thank you. – Vinay Sathyanarayana Aug 15 '11 at 12:43
  • Are you saying that the result of `dir` from a service is `""`? – Gabe Aug 15 '11 at 12:45
  • Exactly. Command exeucted from a service is returning "" string result. – Vinay Sathyanarayana Aug 15 '11 at 12:47

3 Answers3

1

Attach the Visual Studio debugger to your running service (Debug menu, Attach to Process), put a breakpoint in the code where you execute the the command, and then execute the comand. This will enable you to debug the problem. If necessary, you can call the System.Diagnostics.Debugger.Break method in your service to force it to break into the debugger at the appropriate point, without having to attach the debugger first.

Edit:

I suggest you also capture the standard error stream:

procStartInfo.RedirectStandardError = true;
...
string error = proc.StandardError.ReadToEnd(); 
proc.WaitForExit();

Check what you get in the error string. I use WaitForExit to wait until the command exits too.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
  • Thanks for the post. I did this earlier before posting the question in this forum. And thats how I came to know that the service is returning "" when it executes the last line of the above code. :( – Vinay Sathyanarayana Aug 15 '11 at 13:02
1

You are not setting the working Directory (ie procStartInfo.WorkingDirectory), so if the system can not find 'myCommand' , you will get this result.

Even if 'myCommand' is in the same directory as your service exe file, that does not normally work as the working directory for a service is normally windows\system32.

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • Hi sgmoore, thanks for your inputs. However, if you think that working directory may be the cause, then how is the same code working when I use it in a windows forms application? Confused. – Vinay Sathyanarayana Aug 15 '11 at 16:52
  • For windows forms applications, it is more common for your working directory to be set to the folder where the application resides. For example, that would be the default if you were to create a shortcut to an application, or if you were to browse to a folder and double click an application. – sgmoore Aug 15 '11 at 17:03
0

I preferred this Easier way to debug a C# Windows Service

I added the conditional If to launch the debugger from the OnStart method, then put a breakpoint in my OnCustomCommand(int command) method.

Community
  • 1
  • 1
Justin
  • 501
  • 5
  • 13