首页 > Windows开发 > 详细

构建简单Windows Service示例

时间:2019-05-20 23:50:36      阅读:120      评论:0      收藏:0      [点我收藏+]
  1. 示例源码:WindowsServiceSample
  2. ServiceHelper源码:ServiceHelper

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

技术分享图片

2. 配置服务参数

技术分享图片

技术分享图片

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

技术分享图片

实现代码:

    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);
    }
}

构建简单Windows Service示例

原文:https://www.cnblogs.com/MeetYan/p/10897299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!