1.创建windows服务应用

2.右键查看代码

3.写个计时器Timer  using System.Timers;

如上图,按tab键快速操作  会自动创建一个委托

改为下边的方式,打印日志来记录服务运行

  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. using System.Timers;
  12.  
  13. namespace MyFirstWindowsService
  14. {
  15. public partial class Service1 : ServiceBase
  16. {
  17. public Service1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. protected override void OnStart(string[] args)
  23. {
  24. WriteRunLog("服务开始了!!!");
  25. Timer timer = new Timer();
  26. timer.Interval = ;
  27. timer.Elapsed += Timer_Elapsed;
  28. timer.Start();
  29. }
  30.  
  31. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  32. {
  33. WriteRunLog("当前时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"));
  34. }
  35.  
  36. protected override void OnStop()
  37. {
  38. WriteRunLog("服务结束了!!!");
  39. }
  40.  
  41. /// <summary>
  42. /// 记录运行日志
  43. /// </summary>
  44. /// <param name="writeMsg"></param>
  45. public void WriteRunLog(string writeMsg)
  46. {
  47. FIle_Common file = new FIle_Common();
  48. file.CreateDire(@"F:\ServiceLog\");
  49.  
  50. using (FileStream fs = new FileStream(@"F:\ServiceLog\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write))
  51. {
  52. StreamWriter m_streamWriter = new StreamWriter(fs);
  53.  
  54. m_streamWriter.BaseStream.Seek(, SeekOrigin.End);
  55.  
  56. m_streamWriter.WriteLine("mcWindowsService:" + writeMsg + " Time:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
  57.  
  58. m_streamWriter.Flush();
  59.  
  60. m_streamWriter.Close();
  61.  
  62. fs.Close();
  63. }
  64. }
  65. }
  66. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.IO;
  6.  
  7. namespace MyFirstWindowsService
  8. {
  9. public class FIle_Common
  10. {
  11. /// <summary>
  12. /// 创建文件夹
  13. /// </summary>
  14. /// <param name="sender"></param>
  15. /// <param name="e"></param>
  16. public void CreateDire(string path)
  17. {
  18. if (!Directory.Exists(path))
  19. {
  20. Directory.CreateDirectory(path);
  21. }
  22. }
  23. /// <summary>
  24. /// 删除文件夹
  25. /// </summary>
  26. /// <param name="sender"></param>
  27. /// <param name="e"></param>
  28. public void DeleteDire(string path)
  29. {
  30. if (Directory.Exists(path))
  31. {
  32. Directory.Delete(path);
  33. }
  34. }
  35.  
  36. /// <summary>
  37. /// 删除文件
  38. /// </summary>
  39. /// <param name="sender"></param>
  40. /// <param name="e"></param>
  41. public void DeleteDireOne(string path)
  42. {
  43. if (File.Exists(path))
  44. {
  45. File.Delete(path);
  46. }
  47. }
  48. }
  49. }

4.右键添加安装程序

5.我用的VS2019  .net4.8  此时安装组件已经写好了,但是设计图里不显示,下边附上解决方案

6.解决方法:在这个类上边 using System.ServiceProcess;

此时 两个安装组件都显示出来了

7.设置服务安装属性

Description:对服务的说明

DisplayName:向用户标识服务的友好名称

ServiceName:表示在系统服务中的名称

StartType:启动服务的方式和时间,如果为Manual则手动启动,默认停止,如果为Automatic为自动启动

Accout:账户类型,LocalSystem本地系统服务

此时,Windows Service就编写完了

8.安装Windows Service

生成解决方案 进入\bin\Debug 下 添加两个批处理文件来安装 卸载服务

statr.bat    MyFirstWindowsService.exe是\Debug下生成的.exe文件   MyFirstWindowsService是上边设置的ServiceName

  1. %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe %~dp0MyFirstWindowsService.exe
  2. Net Start MyFirstWindowsService
  3. sc config MyFirstWindowsService start= auto
  4. pause

stop.bat

  1. %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u %~dp0MyFirstWindowsService.exe
  2. pause

运行statr.bat

右键 我的电脑  管理

若要卸载该服务,运行stop.bat

windows服务搭建(VS2019创建Windows服务不显示安装组件)的更多相关文章

  1. [Solution] Microsoft Windows 服务(1) C#创建Windows服务

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

  2. 篇章一:SVN服务搭建【基于Windows server 2008R2 + Windows7】

    1.软件下载 1.1 软件介绍 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站 ...

  3. windows下使用IIS创建git服务

    Bonobo Git Server 下载地址: https://bonobogitserver.com/ 安装方法:https://bonobogitserver.com/install/ 配置简单, ...

  4. 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq

    常量,字段,构造方法   常量 1.什么是常量 ​ 常量是值从不变化的符号,在编译之前值就必须确定.编译后,常量值会保存到程序集元数据中.所以,常量必须是编译器识别的基元类型的常量,如:Boolean ...

  5. C#创建Windows Service(Windows 服务)基础教程

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  6. C# windows服务:创建Windows服务(Windows Services)的一般步骤

    C#创建Windows服务(Windows Services) Windows服务在Visual Studio 以前的版本中叫NT服务,在VS.net启用了新的名称.用Visual C# 创建Wind ...

  7. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

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

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

  9. windows下搭建nginx-rtmp服务器

    windows下搭建nginx-rtmp服务器 windows下搭建nginx-rtmp服务器 准备工作 安装MinGW 安装Mercurial 安装strawberryperl 安装nasm 下载n ...

随机推荐

  1. 2018-11-8-WPF-获取下载内容长度

    title author date CreateTime categories WPF 获取下载内容长度 lindexi 2018-11-08 20:18:15 +0800 2018-11-08 20 ...

  2. linux 定时器 API

    内核提供给驱动许多函数来声明, 注册, 以及去除内核定时器. 下列的引用展示了基本的 代码块: #include <linux/timer.h> struct timer_list { / ...

  3. Java中try catch finally执行

    直接上代码实例: public static void main(String[] args) {    System.out.println(test1()); } static int test1 ...

  4. ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(5)

    我参考了bui官网,里面提供了大量的接口案例和效果,之前下载的前端框架完全不需要bootstrap,所以从这一节开始,不再使用bootstrap(当然不想改变的也可以继续使用之前的框架,不影响使用), ...

  5. Java 学习笔记(3)——函数

    之前的几篇文章中,总结了java中的基本语句和基本数据类型等等一系列的最基本的东西,下面就来说说java中的函数部分 函数基础 在C/C++中有普通的全局函数.类成员函数和类的静态函数,而java中所 ...

  6. SNOI2019

    题解: t1: 想了一会才会.. 以为是啥最小表示法之类的..然后这个我又不会 其实只要考虑一下a[i],a[i+1]之间的大小关系就行了 t2: 好像和题解不太一样.. 我的做法比较麻烦.. 枚举A ...

  7. ARM裸机开发之交叉工具链和MakeFile工程管理

    一.交叉工具链 嵌入式Linux开发采用交叉开发,简单来说就是在宿主机(PC机)上面编译出能够在其他硬件平台上面运行的程序.在这个过程中,需要用到许多的交叉工具,这些交叉工具的集合就叫做交叉工具链.下 ...

  8. 29(30).socket网络基础

    转载:https://www.cnblogs.com/linhaifeng/articles/6129246.html 一 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构 互联网 ...

  9. echarts拓扑图(graph,力导向布局图)

    echarts连接:https://gallery.echartsjs.com/editor.html?c=xCLEj67T3H 讲解:https://www.cnblogs.com/koala201 ...

  10. Java8 Date API

    一 .Clock 时钟 Clock类提供了访问当前日期和时间的方法,Clock是时区敏感的,可以用来取代 System.currentTimeMillis() 来获取当前的微秒数.某一个特定的时间点也 ...