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();
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服务及服务的安装、启动、删除、定时执行任务的更多相关文章
- c#创建windows服务(创建,安装,删除)
一.在vs中创建一个window服务 二.进入Service1.cs页面后 右击----创建安装程序,安装程序创建成功后---会出现ProjectInstaller.cs文件 三.进入ProjectI ...
- Windows下Apache的下载安装启动停止
一:下载 打开任意浏览器,输入网址:http://httpd.apache.org/ 进入如下界面: 我们选择最新版Apache httpd 2.4.12Released,点击Download,进入如 ...
- 提示“Web打印服务CLodop未安装启动”的各种原因和解决方法
旧版提示:"CLodop云打印服务(localhost本地)未安装启动!"新版提示:"Web打印服务CLodop未安装启动,点击这里下载执行安装(若此前已安装过,可点这里 ...
- 手把手教用C#编写Windows服务 并控制服务 安装、启动、停止、卸载
Windows服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动, ...
- 编写Windows服务疑问2:探索服务与安装器的关系
首先,来弄两个服务,一个叫“飞机”,一个叫“火车”. public class FeiJiService : ServiceBase { public FeiJiService() { Service ...
- C#编写windows服务
项目要求: 数据库用有一张表,存放待下载文件的地址,服务需要轮训表将未下载的文件下载下来. 表结构如下: 过程: VS--文件-->新建项目-->windows-->windows服 ...
- C#编写Windows 服务
C#编写Windows 服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时 ...
- Windows服务的创建、安装、卸载
1.新建Window服务项目 2.添加安装配置文件 3.serviceProcessInstaller1右键属性,设置Account属性为LocalSystem. serviceInstaller1右 ...
- python实现编写windows服务
使用python编写windows服务 最近测试服务器上经常发生磁盘空间不足,每次手动清除比较麻烦,所以写个windows服务定时清理下.中间也遇到过几个坑,一起记录下来. 1.python实现win ...
随机推荐
- MySQL技巧(二)——无限级分类表设计
无限级分类表的设计(掌握'自身连接') 类似图书这种,会有很多种分类,而且在现实生活中这种分类会无限的往下分,所以不可能每有一个分类就创建一个分类表.应该使用下面这种语句 DROP TABLE IF ...
- Redis配置解读
- 2018最新iOS端界面UI设计规范整理
在iPhone 6还没出的时候,都是用640×1136 px来做设计稿的,自从6的发布,所有的设计稿尺寸以750×1334 px来做设计稿尺寸 以750x1334px作为设计稿标准尺寸的原由: 从中间 ...
- 判断文本是否溢出/hover显示全部
前言 在工作中我们经常会遇到,文字过多,需要用省略号,并且鼠标hover的时候 还需要 显示全部的文字的需求. 正文 文字过多需要用省略号的实现:上代码啦 .ellipsis { width: 100 ...
- 输出映射resultMap
①:编写接口方法 /** * 根据id查询用户 * @param id * @return */ public User queryUserById3(Integer id); ②:编写映射文件 1: ...
- java.net.ProtocolException:unexpected end of stream
原因:php 给android 写接口出现java.net.ProtocolException:unexpected end of stream,查找android方面原因时发现数据超长 ,发现htm ...
- Web前端开发必备
前端学习相关书籍 关于书籍 HTML.CSS 类别书籍,都是大同小异,在当当网.卓越网搜索一下很多推荐.如果感觉学的差不多了,可以关注一下<CSS禅意花园>,这个很有影响力. Javasc ...
- 使用hibernate报错java.lang.ExceptionInInitializerError的处理方法
今天使用hibernate搭建持久层出现一个问题 原因在于 在创建user liberty时同时勾选了System Library(added to the boot class )path
- VRRP技术总结和配置实践
1.VRRP作为网关可靠性的常用方法,基本思路是,两台路由器组成一个虚拟路由器,通过VRRP协议对内网呈现一个虚拟的网关ip, 以便让局域网内部的终端通过这个虚拟网关对外进行通信. 2.VRRP的最简 ...
- Redis常用命令【字符串】
1.启动Redis客户端 进入src目录下,执行:redis-cli启动Redis客户端 2.help 帮助 帮助命令,用来查看redis命令的使用方式 3.set 设置 3.1设置 3.2不存在才设 ...