How to convert a console application to a windows service?

11,327

Inside of your code, you can use the Environment.UserInteractive property to determine which mode you are in console/service.

Example:

static class Program
{
    static void Main(params string[] args)
    {
        var service = new Service1();

        if (!Environment.UserInteractive)
        {
            var servicesToRun = new ServiceBase[] { service };
            ServiceBase.Run(servicesToRun);
            return;
        }

        Console.WriteLine("Running as a Console Application");
        Console.WriteLine(" 1. Run Service");
        Console.WriteLine(" 2. Other Option");
        Console.WriteLine(" 3. Exit");
        Console.Write("Enter Option: ");

        var input = Console.ReadLine();

        switch (input)
        {
            case "1":
                service.Start(args);
                Console.WriteLine("Running Service - Press Enter To Exit");
                Console.ReadLine();
                break;
            case "2":
                break;
        }
        Console.WriteLine("Closing");
    }
}

public partial class Service1 : ServiceBase
{
    public Service1() { InitializeComponent(); }
    public void Start(string[] args) { OnStart(args); }
    protected override void OnStart(string[] args) { }
    protected override void OnStop() { }
}
Share:
11,327
virajs07
Author by

virajs07

Updated on June 04, 2022

Comments

  • virajs07
    virajs07 almost 2 years

    How to convert a console application in C# to a windows service? Is there a template that does it?