一、编写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. 【Java每日一题】20170224

    20170223问题解析请点击今日问题下方的“[Java每日一题]20170224”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; import jav ...

  2. WORLD 目录排版调整

    文本如下: ----------------------------------------------------------------- 前言1 简介2 我爱你3 圣灵丹方士大夫4 阿类似的看风 ...

  3. #WEB安全基础 : HTML/CSS | 0x1初识CSS

    "我受够这些难看的网页了,我怎么才能让它变得好看点?"你说. 我答道:"看来你得学点CSS了" 学习这些东西只有一个原则,就是用你的脑袋想,用你的眼睛看,用的你 ...

  4. 贝塞尔曲线与CSS3动画、SVG和canvas的应用

    简介 贝塞尔曲线是可以做出很多复杂的效果来的,比如弹跳球的复杂动画效果,首先加速下降,停止,然后弹起时逐渐减速的效果. 使用贝塞尔曲线常用的两个网址如下: 缓动函数:http://www.xuanfe ...

  5. win7下利用ftp实现华为路由器的配置文件上传和下载

    win7下利用ftp实现华为路由器的配置文件上传和下载 1.  Win7下ftp的安装和配置 (1)开始—>控制面板—>程序—>程序和功能—>打开或关闭Windows功能 (2 ...

  6. Android的ToolBar

    ToolBar比ActionBar更加可控,自由.因此,Google 逐渐使用ToolBar来代替ActionBar. 使用ToolBar 1.要引入appCompat_v7支持 2.主题设置为NoA ...

  7. Android为TV端助力 转载:android MVC设计模式

    Controller控制器 import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle ...

  8. tornado 模板引擎

    在tornado的模板引擎中,有两种方式,UImethod与UImodule 自定义方法 在模板中调用方法: tornado:与Django一样使用{{}},但是对于for循环之类,Django以{% ...

  9. Kotlin入门(22)适配器的简单优化

    列表视图 为实现各种排列组合类的视图(包括但不限于Spinner.ListView.GridView等等),Android提供了五花八门的适配器用于组装某个规格的数据,常见的适配器有:数组适配器Arr ...

  10. git 入门教程之个性化 git

    前情概要 初识 git 时,我们就已经接触过 git 的基本配置,使用 git config 命令配置用户名和邮箱: # 配置当前项目(`local`)的用户名(`snowdreams1006`) g ...