1. 示例源码:WindowsServiceSample
  2. ServiceHelper源码:ServiceHelper

1. 创建Windows Service项目,如图:

构建简单Windows Service示例

2. 配置服务参数

构建简单Windows Service示例

构建简单Windows Service示例

3. 安装,启动,停止,卸载服务

构建简单Windows Service示例

实现代码:

    private string ServicePath => txtServicePath.Text.Trim();
    private string ServiceName => "ServiceSample";
 
    private void BtnStart_Click(object sender, EventArgs e)
    {
        if (!ServiceHelper.IsExisted(ServiceName))
        {
            MessageBoxHelper.ShowError($"{ServiceName}不存在");
            return;
        }
 
        ServiceHelper.Start(ServiceName);
    }
 
    private void BtnStop_Click(object sender, EventArgs e)
    {
        if (!ServiceHelper.IsExisted(ServiceName))
        {
            MessageBoxHelper.ShowError($"{ServiceName}不存在");
            return;
        }
 
        ServiceHelper.Stop(ServiceName);
    }
 
    private void BtnInstall_Click(object sender, EventArgs e)
    {
        if (ServiceHelper.IsExisted(ServiceName))
        {
            MessageBoxHelper.ShowError($"{ServiceName}已经存在");
            return;
        }
 
        ServiceHelper.Install(ServicePath);
    }
 
    private void BtnUnInstall_Click(object sender, EventArgs e)
    {
        if (!ServiceHelper.IsExisted(ServiceName))
        {
            MessageBoxHelper.ShowError($"{ServiceName}不存在");
            return;
        }
 
        ServiceHelper.Uninstall(ServicePath);
    }
}

相关文章:

  • 2022-02-15
  • 2022-12-23
  • 2021-04-01
  • 2021-04-30
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2021-12-19
  • 2022-01-02
相关资源
相似解决方案