一、编写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();
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

C# 编写windows服务及服务的安装、启动、删除、定时执行任务的更多相关文章

  1. c#创建windows服务(创建,安装,删除)

    一.在vs中创建一个window服务 二.进入Service1.cs页面后 右击----创建安装程序,安装程序创建成功后---会出现ProjectInstaller.cs文件 三.进入ProjectI ...

  2. Windows下Apache的下载安装启动停止

    一:下载 打开任意浏览器,输入网址:http://httpd.apache.org/ 进入如下界面: 我们选择最新版Apache httpd 2.4.12Released,点击Download,进入如 ...

  3. 提示“Web打印服务CLodop未安装启动”的各种原因和解决方法

    旧版提示:"CLodop云打印服务(localhost本地)未安装启动!"新版提示:"Web打印服务CLodop未安装启动,点击这里下载执行安装(若此前已安装过,可点这里 ...

  4. 手把手教用C#编写Windows服务 并控制服务 安装、启动、停止、卸载

    Windows服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动, ...

  5. 编写Windows服务疑问2:探索服务与安装器的关系

    首先,来弄两个服务,一个叫“飞机”,一个叫“火车”. public class FeiJiService : ServiceBase { public FeiJiService() { Service ...

  6. C#编写windows服务

    项目要求: 数据库用有一张表,存放待下载文件的地址,服务需要轮训表将未下载的文件下载下来. 表结构如下: 过程: VS--文件-->新建项目-->windows-->windows服 ...

  7. C#编写Windows 服务

    C#编写Windows 服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时 ...

  8. Windows服务的创建、安装、卸载

    1.新建Window服务项目 2.添加安装配置文件 3.serviceProcessInstaller1右键属性,设置Account属性为LocalSystem. serviceInstaller1右 ...

  9. python实现编写windows服务

    使用python编写windows服务 最近测试服务器上经常发生磁盘空间不足,每次手动清除比较麻烦,所以写个windows服务定时清理下.中间也遇到过几个坑,一起记录下来. 1.python实现win ...

随机推荐

  1. Flask 系列之 LoginManager

    说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过使用 flask-login 进行会话管理的相关操作 ...

  2. vue(二)-父子组件语法

    语法: 子组件: 给子组件弄个名字,name.props用来声明从父组件传来的数据.其他html和css部分正常写. 父组件: HTML部分:使用子组件name作为标签来显示子组件.:items=“i ...

  3. 【 js 工具 】如何在Github Pages搭建自己写的页面?

    最近发现 github 改版了,已没有像原来的 Launch automatic page generator 这样的按钮等,所以我对我的文章也进行了修正,对于新版来说,步骤更加简单了.欢迎享用. - ...

  4. ASPxGridView 用法

    一.ASPxGridView属性:概述设置(Settings) 1.1.Settings <Settings GridLines="Vertical" : 网格样式 Vert ...

  5. es6 语法 (函数扩展)

    //函数参数默认值(more值后不能有参数) { function test(x,y = 'world'){ console.log('默认值',x,y); } test('hello');// he ...

  6. vue从入门到进阶:过滤器filters(五)

    Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 JavaScrip ...

  7. Python 标准类库-Windows特殊服务之msvcrt

    标准类库-Windows特殊服务之msvcrt   by:授客 QQ:1033553122 广告:出售自研自动化小平台(无需编码也可用),有需要请联系 测试环境 win7 64位 Python 3.4 ...

  8. Jump Flood Algorithms for Centroidal Voronoi Tessellation

    Brief Implemented both CPU and GPU version, you could consider this as the basic playground to imple ...

  9. vi中的全局替换

    一.基本语法 替换命令语法: :[addr]s/源字符串/目标字符串/[option] 全局替换: :%s/源字符串/目标字符串/g 参数说明: [addr]--表示检索范围,省略时表示当前行. &q ...

  10. Redis内存数据库快速入门

    Redis简介 Redis是一个开源(BSD许可),内存数据结构存储,用作数据库,缓存和消息代理.它支持数据结构,如 字符串,散列,列表,集合,带有范围查询的排序集,位图,超级日志,具有半径查询和流的 ...