pu369com

C# 编写windows服务及服务的安装、启动、删除、定时执行任务

一、编写windows服务 

1、VS2017  - 创建服务Myservice

2、创建好项目之后 --- >> 双击 Service1.cs  ---- >>  出现一个设计界面   ---->> 右键界面  --- >> 弹出对话框选择  ”添加安装程序“

3、在设计界面修改 serviceProcessInstaller1的属性 Account 为 LocalSystem  (也可用代码修改)

4、在设计界面修改 serviceInstaller1 的属性: display 为  myfirstservice  ;    description 为 我的首个服务  ;   StartType 为 Automatic

5、修改Service1.cs 代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace myservice
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            System.IO.File.AppendAllText(@"D:\Log.txt", " Service Start :" + DateTime.Now.ToString());
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            System.IO.File.AppendAllText(@"D:\Log.txt", " Service Stop :" + DateTime.Now.ToString());
        }
    }
}

6、生成解决方案,可以在项目的dubug目录中找到 myservice.exe

二、SC命令=====安装、开启、配置、关闭windows服务

1、将myservice.exe放在英文目录下,我的是  d:\myservice.exe

2、在cmd中,转到D:并执行以下使命令进行服务安装和启动(这里的myserv 是自定义的名字)

sc create myserv  binPath= "d:/myservice.exe"

sc config  myserv start= auto //(自动) (DEMAND----手动、DISABLED----禁用) 并且start连着= ,而=的后面有一个空格 net start myserv

可以看到d盘下已生成了log.txt

3.删除服务,执行以下代码

  sc delete myserv

4、为方便使用,可编辑为bat批处理文件

@echo.服务启动......  
@echo off  
@sc create myserv1 binPath= "d:\demo.exe"  
@net start myserv1 
@sc config myserv1 start= AUTO  
@echo off  
@echo.启动完毕!  
@pause  

5.或在程序中用以下代码安装(参考http://www.cnblogs.com/pingming/p/5115320.html)

//安装服务
string path = @"D:\demo.exe";
                    Process.Start("sc", "create myDemo binPath= \"" + path + "\" ");
                    Console.WriteLine("安装成功");


//卸载服务
                    Process.Start("sc", "delete KJLMDemo");
                    Console.WriteLine("卸载成功");
                    break;

6、定时执行任务:参考 https://www.cnblogs.com/Beau/p/3491063.html 

//开始事件
        protected override void OnStart(string[] args)
        {
             //定时事件
            MyTimer(); 
        }

        //结束事件
        protected override void OnStop()
        {
            writeLog("服务结束时间:"  + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }


        //实例化System.Timers.Timer   
        private void MyTimer()
        {
            System.Timers.Timer MT = new System.Timers.Timer(30000);
            MT.Elapsed += new System.Timers.ElapsedEventHandler(MTimedEvent);
            MT.Enabled = true;

        }

        //构造System.Timers.Timer实例   间隔时间事件   
        private void MTimedEvent(object source, System.Timers.ElapsedEventArgs e)
        {

            //实现方法
            
        }

 关于定时器,System.Windows.Forms.Timer与System.Timers.Timer的区别,参考这里:http://www.cnblogs.com/lonelyxmas/archive/2009/10/27/1590604.html

posted on 2018-11-16 18:35  pu369com  阅读(1312)  评论(0编辑  收藏  举报

导航