windows服务搭建(VS2019创建Windows服务不显示安装组件)
1.创建windows服务应用
2.右键查看代码
3.写个计时器Timer using System.Timers;
如上图,按tab键快速操作 会自动创建一个委托
改为下边的方式,打印日志来记录服务运行
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- using System.Threading.Tasks;
- using System.Timers;
- namespace MyFirstWindowsService
- {
- public partial class Service1 : ServiceBase
- {
- public Service1()
- {
- InitializeComponent();
- }
- protected override void OnStart(string[] args)
- {
- WriteRunLog("服务开始了!!!");
- Timer timer = new Timer();
- timer.Interval = ;
- timer.Elapsed += Timer_Elapsed;
- timer.Start();
- }
- private void Timer_Elapsed(object sender, ElapsedEventArgs e)
- {
- WriteRunLog("当前时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"));
- }
- protected override void OnStop()
- {
- WriteRunLog("服务结束了!!!");
- }
- /// <summary>
- /// 记录运行日志
- /// </summary>
- /// <param name="writeMsg"></param>
- public void WriteRunLog(string writeMsg)
- {
- FIle_Common file = new FIle_Common();
- file.CreateDire(@"F:\ServiceLog\");
- using (FileStream fs = new FileStream(@"F:\ServiceLog\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write))
- {
- StreamWriter m_streamWriter = new StreamWriter(fs);
- m_streamWriter.BaseStream.Seek(, SeekOrigin.End);
- m_streamWriter.WriteLine("mcWindowsService:" + writeMsg + " Time:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
- m_streamWriter.Flush();
- m_streamWriter.Close();
- fs.Close();
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.IO;
- namespace MyFirstWindowsService
- {
- public class FIle_Common
- {
- /// <summary>
- /// 创建文件夹
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void CreateDire(string path)
- {
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- }
- /// <summary>
- /// 删除文件夹
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void DeleteDire(string path)
- {
- if (Directory.Exists(path))
- {
- Directory.Delete(path);
- }
- }
- /// <summary>
- /// 删除文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public void DeleteDireOne(string path)
- {
- if (File.Exists(path))
- {
- File.Delete(path);
- }
- }
- }
- }
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
- %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe %~dp0MyFirstWindowsService.exe
- Net Start MyFirstWindowsService
- sc config MyFirstWindowsService start= auto
- pause
stop.bat
- %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u %~dp0MyFirstWindowsService.exe
- pause
运行statr.bat
右键 我的电脑 管理
若要卸载该服务,运行stop.bat
windows服务搭建(VS2019创建Windows服务不显示安装组件)的更多相关文章
- [Solution] Microsoft Windows 服务(1) C#创建Windows服务
Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而 ...
- 篇章一:SVN服务搭建【基于Windows server 2008R2 + Windows7】
1.软件下载 1.1 软件介绍 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站 ...
- windows下使用IIS创建git服务
Bonobo Git Server 下载地址: https://bonobogitserver.com/ 安装方法:https://bonobogitserver.com/install/ 配置简单, ...
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
常量,字段,构造方法 常量 1.什么是常量 常量是值从不变化的符号,在编译之前值就必须确定.编译后,常量值会保存到程序集元数据中.所以,常量必须是编译器识别的基元类型的常量,如:Boolean ...
- C#创建Windows Service(Windows 服务)基础教程
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- C# windows服务:创建Windows服务(Windows Services)的一般步骤
C#创建Windows服务(Windows Services) Windows服务在Visual Studio 以前的版本中叫NT服务,在VS.net启用了新的名称.用Visual C# 创建Wind ...
- C# 创建Windows Service(Windows服务)程序
本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...
- c#创建windows服务(创建,安装,删除)
一.在vs中创建一个window服务 二.进入Service1.cs页面后 右击----创建安装程序,安装程序创建成功后---会出现ProjectInstaller.cs文件 三.进入ProjectI ...
- windows下搭建nginx-rtmp服务器
windows下搭建nginx-rtmp服务器 windows下搭建nginx-rtmp服务器 准备工作 安装MinGW 安装Mercurial 安装strawberryperl 安装nasm 下载n ...
随机推荐
- 2018-11-8-WPF-获取下载内容长度
title author date CreateTime categories WPF 获取下载内容长度 lindexi 2018-11-08 20:18:15 +0800 2018-11-08 20 ...
- linux 定时器 API
内核提供给驱动许多函数来声明, 注册, 以及去除内核定时器. 下列的引用展示了基本的 代码块: #include <linux/timer.h> struct timer_list { / ...
- Java中try catch finally执行
直接上代码实例: public static void main(String[] args) { System.out.println(test1()); } static int test1 ...
- ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统(5)
我参考了bui官网,里面提供了大量的接口案例和效果,之前下载的前端框架完全不需要bootstrap,所以从这一节开始,不再使用bootstrap(当然不想改变的也可以继续使用之前的框架,不影响使用), ...
- Java 学习笔记(3)——函数
之前的几篇文章中,总结了java中的基本语句和基本数据类型等等一系列的最基本的东西,下面就来说说java中的函数部分 函数基础 在C/C++中有普通的全局函数.类成员函数和类的静态函数,而java中所 ...
- SNOI2019
题解: t1: 想了一会才会.. 以为是啥最小表示法之类的..然后这个我又不会 其实只要考虑一下a[i],a[i+1]之间的大小关系就行了 t2: 好像和题解不太一样.. 我的做法比较麻烦.. 枚举A ...
- ARM裸机开发之交叉工具链和MakeFile工程管理
一.交叉工具链 嵌入式Linux开发采用交叉开发,简单来说就是在宿主机(PC机)上面编译出能够在其他硬件平台上面运行的程序.在这个过程中,需要用到许多的交叉工具,这些交叉工具的集合就叫做交叉工具链.下 ...
- 29(30).socket网络基础
转载:https://www.cnblogs.com/linhaifeng/articles/6129246.html 一 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构 互联网 ...
- echarts拓扑图(graph,力导向布局图)
echarts连接:https://gallery.echartsjs.com/editor.html?c=xCLEj67T3H 讲解:https://www.cnblogs.com/koala201 ...
- Java8 Date API
一 .Clock 时钟 Clock类提供了访问当前日期和时间的方法,Clock是时区敏感的,可以用来取代 System.currentTimeMillis() 来获取当前的微秒数.某一个特定的时间点也 ...