使用C#编程语言开发Windows Service服务
转载-https://www.cnblogs.com/yubao/p/8443455.html
Create Windows Service project using Visual Studio

Create files on starting and stopping service:

using System.IO;
protected override void OnStart(string[] args)
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
} protected override void OnStop()
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
}

Try to run Windows Program on Debug mode
Click "Start" directly:

How to create a service use exe file

C:\Windows\system32>sc create -h
DESCRIPTION:
Creates a service entry in the registry and Service Database.
USAGE:
sc <server> create [service name] [binPath= ] <option1> <option2>... OPTIONS:
NOTE: The option name includes the equal sign.
A space is required between the equal sign and the value.
type= <own|share|interact|kernel|filesys|rec|userown|usershare>
(default = own)
start= <boot|system|auto|demand|disabled|delayed-auto>
(default = demand)
error= <normal|severe|critical|ignore>
(default = normal)
binPath= <BinaryPathName to the .exe file>
group= <LoadOrderGroup>
tag= <yes|no>
depend= <Dependencies(separated by / (forward slash))>
obj= <AccountName|ObjectName>
(default = LocalSystem)
DisplayName= <display name>
password= <password>

Start service use "sc create" command
C:\Windows\system32>sc create MyService binpath="C:\Users\yubao\Documents\Visual Studio 2017\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe" start=auto
[SC] CreateService SUCCESS
Start service:

Delete Service use "sc delete" command:
C:\Windows\system32>sc delete MyService
[SC] DeleteService SUCCESS
Install Service on Windows



Install Service to Windows use InstallUtil.exe:
Where InstallUtil.exe is:
for win32: C:\Windows\Microsoft.NET\Framework\v4.0.30319
for win64: C:\Windows\Microsoft.NET\Framework64\v4.0.30319
Install Service:
C:\Windows\system32>InstallUtil.exe C:\Users\yubao\Documents\Visual Studio 2017\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe
Uninstall Service:
C:\Windows\system32>InstallUtil.exe /u "C:\Users\yubao\Documents\Visual Studio 2017\Projects\WindowsService1\WindowsService1\bin\x64\Release\WindowsService1.exe"
Use Setup Project to Install windows service






Build Setup Project to generate Installer files:

Double click "Setup.exe/msi" to start installation. And then you will see this typicall error for x64 target platform: ( for win32, you will never see this error)

How to solve E1001 under x64 target system:
Download orca.exe from ORCA MSI Editor Standalone Download
Orca MSI Editor allows you to edit the properties of any MSI file. With this tool, you can change the title and text within the installer an look at how and where the files are delivered. It’s also handy if you need to “hack” an MSI to work with a newer version of Windows.
Edit .msi file with "orca.exe " tool:


modify binary path to "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtilLib.dll"


Final install path:

How to Debug Windows Service program on Visual Studio:
Service1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO; namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} public void OnDebug()
{
string[] str = { "0" };
OnStart(str);
} protected override void OnStart(string[] args)
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt");
} protected override void OnStop()
{
File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStop.txt");
} }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace WindowsService1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Console.WriteLine("This is debug mode");
Service1 myService = new Service1();
myService.OnDebug();
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif }
}
}

How to make service auto start after installation:
ProjectInstaller.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Threading.Tasks;
namespace WindowsService1
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
new ServiceController(serviceInstaller1.ServiceName).Start();
}
}
}

使用C#编程语言开发Windows Service服务的更多相关文章
- 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……
过场CG: 接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...
- .NET开发Windows Service程序 - Topshelf
在实际项目开发过程中,会经常写一些类似定时检查,应用监控的应用.这类应用在windows平台通常都会写成window service程序. 在百度上搜索一下'c#开发windows service', ...
- C# Windows Service服务的创建和调试
前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...
- quartz.net结合Topshelf实现windows service服务托管的作业调度框架
topshelf可以很简单方便的实现windows service服务,详见我的一篇博客的介绍 http://www.cnblogs.com/xiaopotian/articles/5428361.h ...
- .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用
Windows Service(服务) 是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...
- ubuntu下安装 gSOAP 用于C/C++开发web service服务端与客户端
昨天在ubuntu下进行安装gSOAP,费了很多时间,没成功,今天又来找了大量教程资料,终于一次成功,这里写下自己的安装步骤和方法,供大家参考. 首先下载gsoap,我下载的是gsoap-2.8.1. ...
- Windows Service 服务搭配FluentScheduler实现定时任务调度
Windows Service 服务 创建Windows Service 项目 创建一个Windows Service项目,并将项目名称改为 TaskWindowService 在解决方案资源管理器内 ...
- 使用CXF开发Web Service服务
1.使用CXF开发Web Service服务端 1.1 开发一个Web Service业务接口,该接口要用@WebService修饰 (1)创建一个Java项目MyServer (2)在MyServe ...
- [开发笔记]-Windows Service服务相关注意事项
注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...
随机推荐
- synchronized锁及其锁升级
点赞再看,养成习惯,微信搜索「小大白日志」关注这个搬砖人. 文章不定期同步公众号,还有各种一线大厂面试原题.我的学习系列笔记. 多线程加锁有两种方式 利用Sychronized关键字 利用Lock接口 ...
- screen使用小结
目录 安装 shell-screen-window关系 常用参数 快捷键 离开当前screen 打开一个新的窗口 查看窗口列表 窗口的快速切换 回到行首 关闭窗口 关闭所有窗口 关闭screen 删除 ...
- Java-GUI 编程之 Swing
Swing概述 实际使用 Java 开发图形界面程序时 ,很少使用 AWT 组件,绝大部分时候都是用 Swing 组件开发的 . Swing是由100%纯 Java实现的,不再依赖于本地平台的 GU ...
- 【干货】BIOS、UEFI、MBR、GPT、GRUB 到底是什么意思?
公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ 01 前言 在学习 Linux 系统启动原理之前,我们先了解下与操作系统启动相关的几个概念. 02 与操作系统启动相关的几个 ...
- 操作系统深度研究(75页PPT)
上一篇:命令行版的斗地主你玩过没? 内容覆盖操作系统基本概念.分类.关键技术,体系架构,发展历程和主流国产操作系统厂商分析. 文中报告节选自兴业证券经济与金融研究院已公开发布研究报告,具体报告内容及相 ...
- 一文说透 MySQL JSON 数据类型(收藏)
JSON 数据类型是 MySQL 5.7.8 开始支持的.在此之前,只能通过字符类型(CHAR,VARCHAR 或 TEXT )来保存 JSON 文档. 相对字符类型,原生的 JSON 类型具有以下优 ...
- 老生常谈系列之Aop--Spring Aop源码解析(一)
老生常谈系列之Aop--Spring Aop源码解析(一) 前言 上一篇文章老生常谈系列之Aop--Spring Aop原理浅析大概阐述了动态代理的相关知识,并且最后的图给了一个Spring Aop实 ...
- 有关 ThreadLocal 的一切
早上好,各位新老读者们,我是七淅(xī). 今天和大家分享的是面试常驻嘉宾:ThreadLocal 当初鹅厂一面就有问到它,问题的答案在下面正文的第 2 点. 1. 底层结构 ThreadLocal ...
- python之贪婪算法
贪婪算法 贪婪算法也称为最优算法,这种算法并不是最准确的答案,但确认最接近答案的近似算法. 这时候有人会问,不是最准确的答案我要她干嘛?但是在日常中,我们有时候会遇到一些我们无法处理的问题,甚至是要花 ...
- 用python实现自动化登录禅道系统 设置定时器自动执行脚本
由于各种原因,我想试下用python实现自动登录禅道系统,并且每天定时执行.(本人第一次接触自动化,在大佬眼中门槛都没摸到的类型) 首先缕清思路: 1.实现自动登录禅道系统,用selenium实现2. ...