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 ...
随机推荐
- 陈志生:德国信贷工厂风控模式对P2P的启发
上海合盘金融信息服务股份有限公司董事长陈志生 和讯银行消息 "2014中国金融论坛"于5月14-15日在北京召开,本次论坛主题为“全面深化金融体制改革与实体经济增长”.和讯网作为指 ...
- Redux 初始化完整结构
文件管理 目录文档 ★★★index.js★★★ ★★★app.js★★★ ★★★store->index.js★★★ ★★★actions->index.js★★★ ★★★store-& ...
- Python--day37--多进程
1,创建多进程(父进程和子进程) import os import time #多进程都要导入multiprocessing from multiprocessing import Process d ...
- H3C 静态路由配置示例
- dotnet core 通过 frp 发布自己的网站
很多时候写出来的网站只能自己内网访问,本文告诉大家如何通过 Frp 将自己的 asp dotnet core 网站发布到外网,让小伙伴访问自己的网站 通过 frp 的方式,可以解决自己的服务器性能太差 ...
- linux 每-CPU 的变量
每-CPU 变量是一个有趣的 2.6 内核的特性. 当你创建一个每-CPU 变量, 系统中每个处理 器获得它自己的这个变量拷贝. 这个可能象一个想做的奇怪的事情, 但是它有自己的优点. 存取每-CPU ...
- JQuery仿购物网站放大镜特效所遇问题及思考
JQuery仿购物网站放大镜特效所遇问题及思考 先贴下效果图,然后描述起来也就不会不知道我在说什么了. 我碰到的问题一: 一开始我自己总结了是因为两个小原因导致的①使用了mouseover,mouse ...
- 学习python资料
资料链接:https://www.cnblogs.com/wupeiqi/articles/5433893.html
- 图解Go里面的sync.Map了解编程语言核心实现源码
基础筑基 在大多数语言中原始map都不是一个线程安全的数据结构,那如果要在多个线程或者goroutine中对线程进行更改就需要加锁,除了加1个大锁,不同的语言还有不同的优化方式, 像在java和go这 ...
- Eclipse+Tomcat+MAVEN开发环境配置
https://blog.csdn.net/zhshulin/article/details/30779873 补充: 1.eclipse配置tomcat window——preferences——s ...