1

I have a C# windows service. In that service code, I define Main() and capture various arguments. If the arg is "install" I call code to install the service. Note that when the arg is install, I am not running the service, just installing it, so it seems to me that this is nothing more than a console app at that point.

When I debug the code in vs.net 2012, I see all of my Console.WriteLine() output in the debug window. I believe that is because VS.NET maps STDOUT to the debug window.

But if I open a command prompt and run the command line myservice.exe install, there is no output to the console window. If I run myservice.exe install > out.txt I see all of the output in out.txt. What happened and how can I get that Main() code to actually send the output to the console window?

slolife
  • 19,520
  • 20
  • 78
  • 121

1 Answers1

4

This could be due to the Output Type of your project; please note that the default Windows Application output type does not automatically show a console window. For this, you may want to change the Output Type to a Console Application.

Efran Cobisi
  • 6,138
  • 22
  • 22
  • Okay, that did the trick. I'd like a little more info about what changes when I move my windows service project from a windows app to a console app? Does a windows service app need to be a Windows App? Note that I wasn't launch the exe by double clicking or anything. I was in a console and typed in the command line. I thought that any Console.WriteLine() calls would go to the console or STDOUT. How can I programatically change that? – slolife Mar 22 '13 at 16:22
  • 2
    Afaik, moving to a console application will just set a flag in the PE header of your assembly, which will instruct the loader to create and attach the process to the standard streams upon load. A Windows service does NOT need to be a Windows application. – Efran Cobisi Mar 22 '13 at 16:41
  • There is also a [trick](http://stackoverflow.com/a/7827950/904178) which basically uses a couple of Win32 APIs to attach to a console window, even for non-console applications, but I would stay away if possible. – Efran Cobisi Mar 22 '13 at 16:43