一、编写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. JavaWeb学习日记----XML基础

    1.XML基础: XML全称为eXtensible Markup Language;即可扩展标记型语言,同HTML一样使用标签来操作.它的可扩展性体现在标签可以由自己定义,可以是中文标签. XML用途 ...

  2. HTML5标签选择,图文混排使用dl dt dd

    图文混排,可以使用 dl dt dd(dd 和 dt 是同级,不可以嵌套,没有先后顺序) 1,上面红色部分是标题,可以使用h1里面包含一个span标签,样式一样,所以两个可以一起写. 2,上面黑色部分 ...

  3. CSS 定位与Z-index

    position: static   Z-index 固定是0 position: absolute/relative/fixed   Z-index 有效 在层叠显示上,所有static定位元素看作 ...

  4. 微信小程序中如何获取for循环的item相关值到JS页面的问题

    今天小程序开发过程中,遇到了这个棘手的问题.由于我没有前端基础,只是知道一点儿基本的HTML标签,所以卡了好久,特此分享,望后来的你,可以有所收获. measure  step 1  *.WXML: ...

  5. java StringBuffer读写文件

    java StringBuffer读写文件 StringBuffer的优势 较String:String每更新一次就会new一个新的对象出来,更新次数上去之后,内存开销太大.而StringBuffer ...

  6. 使用Visual Studio Team Services敏捷规划和项目组合管理(七)——流程定制

    使用Visual Studio Team Services敏捷规划和项目组合管理(七)--流程定制 在Team Services中,可以通过流程定制工作追踪体验.流程定义了工作项跟踪系统的构建部分,以 ...

  7. 关于正餐智能POS6.0.1.1改版后,订单模块无法进行部分退款的FAQ

    适用版本:智能POS正餐V6.0.1.1+ 适用情况:订单模块,无法输入自定义金额进行部分退款. 原因:为让报表统计的数据更准确. 改版之后仍可适用部分退款的情况: 1.口碑先付订单,可在口碑模块,选 ...

  8. SQL Agent Job 报“Access to the remote server is denied because the current security context is not trusted”

    SQL Server 2005(Microsoft SQL Server 2005 - 9.00.5000.00)下的一个作业执行一个存储过程,存储过程中动态SQL语句使用链接服务器(Linked S ...

  9. Oracle EBS FORM lov

    存在一种情况: 一个LOV的值当前有效,因此填入保存.但突然无效后,当查询该界面时就会弹出LOV框使其修改. 解决方案: 1. 非常粗暴,不设置校验,在LOV对应的item强行将校验设置为NO. 2. ...

  10. selenium驱动程序下载和使用流程

    转自https://blog.csdn.net/weixin_42660771/article/details/81286982 1.下载地址    https://github.com/mozill ...