# C# .NET平台开发windows 服务程序

开发windows服务程序,以使用 sc create 命令来安装服务

安装包

创建一个 net7 net8 控制台程序,NuGet安装以下包

C#
System.ServiceProcess.ServiceController

创建服务启动代码

C#
partial class MyService : ServiceBase
{
    public MyService(string[] args)
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
    }
    protected override void OnStop()
    {
    }
}

程序入口

C#
internal class Program
{
    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new MyService(args)
        };
        ServiceBase.Run(ServicesToRun);
    }
}