如何将控制台应用程序转换为Windows服务?

vir*_*s07 3 c# windows-services console-application

如何将C#中的控制台应用程序转换为Windows服务?是否有模板可以做到这一点?

Erw*_*win 8

在代码内部,您可以使用Environment.UserInteractive属性来确定您在控制台/服务中的模式.

例:

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() { }
}
Run Code Online (Sandbox Code Playgroud)