转载自:http://www.cnblogs.com/xujie/p/5695673.html

1、新建windows服务项目,我这里选择的是Framework4.0,没有选择高版本是为了防止在服务在一些低版本系统上无法正常运行。

2、添加Windows服务的安装程序。

在默认Service1设计器界面空白处点击右键->添加安装程序,系统会自动新建一个带有默认配置的安装程序类,如下图:

新建完安装程序后,需要给默认的serviceInstaller1和serviceProcessInstaller1做一些基本的属性设置。如下图:

以上工作完成,安装程序配置完毕。

注意:如果不给服务添加安装程序,后面是没法把服务安装至windows系统里的。

3、添加应用程序配置文件(如果有需要的话)。

如果项目有需要,一些应用程序的配置参数可以设置在此文件里(例如:数据库连接字符串)。

4、编写windows服务主代码

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.ComponentModel;
  4.  
    using System.Data;
  5.  
    using System.Diagnostics;
  6.  
    using System.IO;
  7.  
    using System.Linq;
  8.  
    using System.ServiceProcess;
  9.  
    using System.Text;
  10.  
    using System.Threading.Tasks;
  11.  
     
  12.  
    namespace WinServiceTest
  13.  
    {
  14.  
    public partial class Service1 : ServiceBase
  15.  
    {
  16.  
    public Service1()
  17.  
    {
  18.  
    InitializeComponent();
  19.  
     
  20.  
    System.Timers.Timer timer = new System.Timers.Timer();
  21.  
    timer.Elapsed += new System.Timers.ElapsedEventHandler(TimedEvent);
  22.  
    timer.Interval = 5000;//每5秒执行一次
  23.  
    timer.Enabled = true;
  24.  
    }
  25.  
    public int count = 0;
  26.  
    //定时执行事件
  27.  
    private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e)
  28.  
    {
  29.  
    //业务逻辑代码
  30.  
    EmailClass mail = new EmailClass();
  31.  
     
  32.  
    mail.Email(count++);
  33.  
    }
  34.  
     
  35.  
    protected override void OnStart(string[] args)
  36.  
    {
  37.  
    this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n");
  38.  
    this.WriteLog("客户端数据同步服务:【服务启动】");
  39.  
    }
  40.  
     
  41.  
    protected override void OnStop()
  42.  
    {
  43.  
    this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss")+ "\n");
  44.  
    this.WriteLog("客户端数据同步服务:【服务停止】");
  45.  
    }
  46.  
    protected override void OnShutdown()
  47.  
    {
  48.  
    this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n");
  49.  
    this.WriteLog("客户端数据同步服务:【计算机关闭】");
  50.  
    }
  51.  
     
  52.  
    #region 记录日志
  53.  
    /// <summary>
  54.  
    /// 记录日志
  55.  
    /// </summary>
  56.  
    /// <param name="msg"></param>
  57.  
    private void WriteLog(string msg)
  58.  
    {
  59.  
     
  60.  
    //string path = @"C:\log.txt";
  61.  
     
  62.  
    //该日志文件会存在windows服务程序目录下
  63.  
    string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";
  64.  
    FileInfo file = new FileInfo(path);
  65.  
    if (!file.Exists)
  66.  
    {
  67.  
    FileStream fs;
  68.  
    fs = File.Create(path);
  69.  
    fs.Close();
  70.  
    }
  71.  
     
  72.  
    using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
  73.  
    {
  74.  
    using (StreamWriter sw = new StreamWriter(fs))
  75.  
    {
  76.  
    sw.WriteLine(DateTime.Now.ToString() + " " + msg);
  77.  
    }
  78.  
    }
  79.  
    }
  80.  
    #endregion
  81.  
    }
  82.  
    }

4、安装与卸载服务

readme里面的内容
  1.  
    请将【WinServiceTest】拷贝到D盘或C盘根目录;
  2.  
    安装服务【管理员身份】运行【SC安装-发送邮件】即可;
  3.  
    卸载服务【管理员身份】运行【SC卸载】即可;

SC安装-发送邮件:

  1.  
    @echo.请稍等,服务启动......
  2.  
    @echo off
  3.  
    @sc create GX_To_EMAIL binPath= "D:\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe"
  4.  
    DisplayName=每隔一段时间发送邮件的服务 start= auto
  5.  
    @sc description GX_To_EMAIL 定时发送邮件
  6.  
    @sc start GX_To_EMAIL
  7.  
    @echo off
  8.  
    @echo.启动完毕!
  9.  
    @pause

SC卸载:

  1.  
    @echo.服务卸载......
  2.  
    @echo off
  3.  
    @sc stop GX_To_EMAIL
  4.  
    @sc delete GX_To_EMAIL
  5.  
    @sc stop GX_To_EMAIL
  6.  
    @echo off
  7.  
    @echo.卸载完毕!
  8.  
    @pause

用 vs 2017创建 windows 服务的更多相关文章

  1. 用C#创建Windows服务(Windows Services)

    用C#创建Windows服务(Windows Services) 学习:  第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...

  2. 玩转Windows服务系列——创建Windows服务

    创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Windows服务项目. 生成的解决方案包含两个项目:Servi ...

  3. .Net创建windows服务入门

    本文主要记录学习.net 如何创建windows服务. 1.创建一个Windows服务程序 2.新建安装程序 3.修改service文件 代码如下 protected override void On ...

  4. C# 创建Windows服务

    创建windows服务项目   2 右键点击Service1.cs,查看代码, 用于编写操作逻辑代码 3 代码中OnStart用于执行服务事件,一般采用线程方式执行方法,便于隔一段事件执行一回 END ...

  5. 使用Topshelf创建Windows服务

    概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...

  6. [转]C#创建Windows服务与安装

    本文档用于创建windows服务说明,使用vs2010系统平台 创建项目 1 创建windows服务项目 2 右键点击Service1.cs,查看代码, 用于编写操作逻辑代码 3 代码中OnStart ...

  7. [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务

    除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...

  8. 在64位windows下使用instsrv.exe和srvany.exe创建windows服务

    在64位windows下使用instsrv.exe和srvany.exe创建windows服务   在32位的windows下,包括windows7,windows xp以及windows 2003, ...

  9. 使用Topshelf 5步创建Windows 服务 z

    使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with T ...

随机推荐

  1. 【校招面试 之 C/C++】第3题 为什么要内存对齐?以及内存对齐的方式

    1.为什么要进行内存对? 参考:https://blog.csdn.net/a369000753/article/details/51188915 所谓内存对齐,是为了让内存存取更有效率而采用的一种编 ...

  2. python之面向对象之类变量和实例变量

    #Auther Bob #--*--conding:utf-8 --*-- #这里的变量全部都是实例变量 class Role(object): def __init__(self,name,role ...

  3. 在ecplise中创建一个maven工程

    1.我们首先需要在Ecplise中配置maven环境,详情见我的博客:https://www.cnblogs.com/wyhluckdog/p/10277278.html 2.maven projec ...

  4. [Selenium]对弹出的Alert窗口进行操作

    Alert alert = driver.switchTo().alert(); alert.accept();

  5. Netty 系列目录

    Netty 系列目录 二 Netty 源码分析(4.1.20) 1.1 Netty 源码(一)Netty 组件简介 2.1 Netty 源码(一)服务端启动 2.2 Netty 源码(二)客户端启动 ...

  6. Spring.NET学习笔记7——依赖对象的注入(基础篇) Level 200

    1.person类 public class Person    {        public string Name { get; set; }        public int Age { g ...

  7. python函数嵌套的实用技术

    def fun(): def fun1(): print () fun1() fun() fun1()#总结老男孩python里面讲过,这个是函数的嵌套,很有用, #效果就是给函数一个自己的小函数.然 ...

  8. css样式记忆

    text-indent: 2em;   //开头空两格: display : none;    //隐藏元素 background:#CCC;  //背景颜色 background: url(imag ...

  9. 二进制搭建kubernetes多master集群【四、配置k8s node】

    上一篇我们部署了kubernetes的master集群,参考:二进制搭建kubernetes多master集群[三.配置k8s master及高可用] 本文在以下主机上操作部署k8s node k8s ...

  10. 2018.07.07 BZOJ2212: Poi2011Tree Rotations(线段树合并)

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MB Description Byteasar the garde ...