C # Windows app and console app in one

We create Windows services all the time and we have a pretty good template, with nice little command line options for easy installation, etc.

Here's what we'd like to do ... Write one bit of code so that the compiled code can run as a service or a console application depending on how you started it.

We found that doing:

static void Main(string[] args)
{
    if (System.Environment.UserInteractive)
    {
        // You double clicked the exe
        Console.Write("You double clicked me")
    }
    else // Windows started me as a service.
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new Service_Manager() };
        ServiceBase.Run(ServicesToRun);
    }
}

      

There was a lattice way, for example to install a service, etc. However, the console is not displayed. So, without writing two applications and sharing the DLL, is there an easy way that or something that can be included that allows the "console" to be shown?

+3


source to share


1 answer


You can use TopShelf if you like, this is exactly what you are looking for.

You can write an application that is actually a Windows Srevice, but that can act like a console application.



This makes debugging easier

0


source







All Articles