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 ...
随机推荐
- Django入门4--admin
python3选择__str__(self),python2选择__unicode__(self):
- js基础——对象和数组
1.Object类型 1)使用new运算符 var box = new Object();===>等同于 var box = Object();(省略new关键字) box.name ...
- <Catalan>杨辉三角实现卡特兰数计算方法
h(n)=C(2n,n)-C(2n,n-1) #include<cstdio> #define siz 20 using namespace std; int n; ][siz]; int ...
- Visual Studio Team Services使用教程【6】:Readers tfs组checkin权限修改
你也可以只开启部分代码的权限 把上面开启的整个应用的权限先去掉 只开启一个文件的权限 2017.4.23之后建议朋友看下面的帖子 TFS2017 & VSTS 实战(繁体中文视频) Visua ...
- python文件的读写追加等操作
# encoding:utf-8 # 文件读取操作 fp=open("E:\\file.txt","r",encoding="utf-8" ...
- 【算法随记七】巧用SIMD指令实现急速的字节流按位反转算法。
字节按位反转算法,在有些算法加密或者一些特殊的场合有着较为重要的应用,其速度也是一个非常关键的应用,比如一个byte变量a = 3,其二进制表示为00000011,进行按位反转后的结果即为110000 ...
- 【温故知新】Java web 开发(二)Servlet 和 简单JSP
系列一介绍了新建一个 web 项目的基本步骤,系列二就准备介绍下基本的 jsp 和 servlet 使用. (关于jsp的编译指令.动作指令.内置对象不在本文讨论范围之内) 1. 首先,在 pom. ...
- 从零开始のcocos2dx生活(四)ActionManager
文章目录 初始化构造函数 析构函数 删除哈希元素 分配存放动作对象的空间 通过索引移除动作 暂停动作 恢复动作 暂停所有的动作 恢复所有的动作 添加动作 移除所有的动作 移除target中的所有动作 ...
- MyBatis原理-延迟加载,一级缓存,二级缓存设置
一.延迟加载 resultMap中的association和collection标签具有延迟加载的功能. 延迟加载的意思是说,在关联查询时,利用延迟加载,先加载主信息.使用关联信息时再去加载关联信息. ...
- 动态规划之用最少的字符操作将字符串A转换为字符串B
1.试用动态规划算法实现下列问题:设A和B是两个字符串.我们要用最少的字符操作,将字符串A转换为字符串B,这里所说的字符操作包括: (1)删除一个字符. (2)插入一个字符. (3)将一个字符改为另一 ...