Windows Service 学习系列(一):建立简单的Windows service
参考:https://www.cnblogs.com/cncc/p/7170951.html
一、开发环境
操作系统:Windows 7 X64
开发环境:VS2017
编程语言:C#
.NET版本:.NET Framework 4.6.1
目标平台:Any CUP
二、创建Windows Service
1、新建一个Windows Service,并将项目名称改为“WindowsService1”,如下图所示:

2、进入“Service1”设计界面,在空白位置右击鼠标弹出上下文菜单,选中“添加安装程序”,如下图所示:

3、此时软件会生成两个组件,分别为“serviceInstaller1”及“serviceProcessInstaller1”,如下图所示:

4、设置“serviceInstaller1”及“serviceProcessInstaller1”的属性


5、至此,Windows服务已经创建完毕。
三、创建安装、启动、停止、卸载服务的Windows窗体
1、在同一个解决方案里新建一个Windows Form项目,并命名为WindowsFormsApp1,如下图所示:

2、将该项目设置为启动项目,并在窗体内添加四个按钮,分别为安装服务、启动服务、停止服务及卸载服务,如下图所示:

3、按下F7进入代码编辑界面,引用“System.ServiceProcess”及“System.Configuration.Install”,并输入如下代码:
using System;
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Windows.Forms; namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string serviceFilePath = $"{Application.StartupPath}\\WindowsService1.exe";
string serviceName = "Service1"; /// <summary>
/// 安装服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.UninstallService(serviceFilePath);
}
this.InstallService(serviceFilePath);
} /// <summary>
/// 启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStart(serviceName);
}
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
}
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
} #region 方法
/// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
private bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
} /// <summary>
/// 安装服务
/// </summary>
/// <param name="serviceFilePath"></param>
private void InstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
}
} /// <summary>
/// 卸载服务
/// </summary>
/// <param name="serviceFilePath"></param>
private void UninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
}
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="serviceName"></param>
private void ServiceStart(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
}
}
} /// <summary>
/// 停止服务
/// </summary>
/// <param name="serviceName"></param>
private void ServiceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
}
#endregion
}
}
4、为了后续调试服务及安装卸载服务的需要,将已生成的MyWindowsService.exe引用到本Windows窗体,如下图所示:

5、由于需要安装服务,故需要使用UAC中Administrator的权限,鼠标右击项目“WindowsServiceClient”,在弹出的上下文菜单中选择“添加”->“新建项”,在弹出的选择窗体中选择“应用程序清单文件”并单击确定,如下图所示:

6、打开该文件,并将<requestedExecutionLevel level="asInvoker" uiAccess="false" />改为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />,如下图所示:

Windows Service 学习系列(一):建立简单的Windows service的更多相关文章
- Windows程序设计学习笔记(1):一个简单的windows程序
<Windows程序设计>(第五版)(美Charles Petzold著) #include<windows.h> LRESULT CALLBACK WndProc(HWND, ...
- Docker学习系列(一):windows下安装docker(转载)
本文目录如下: windows按照docker的基本要求 具体安装步骤 开始使用 安装远程连接工具连接docker 安装中遇到的问题 Docker的更新 Docker中的jupyter windows ...
- 《windows核心编程系列》十八谈谈windows钩子
windows应用程序是基于消息驱动的.各种应用程序对各种消息作出响应从而实现各种功能. windows钩子是windows消息处理机制的一个监视点,通过安装钩子能够达到监视指定窗体某种类型的消息的功 ...
- Windows Service 学习系列(二):C# windows服务:安装、卸载、启动和停止Windows Service几种方式
一.通过InstallUtil.exe安装.卸载.启动.停止Windows Service 方法一 1.以管理员身份运行cmd 2.安装windows服务 切换cd C:\Windows\Micros ...
- Windows Service 学习系列(三)——循环引擎 ICycleEngine
摘要:转载:https://www.cnblogs.com/zhuweisky/archive/2009/09/01/1557792.html#undefined 1.缘起: 有些系统需要每隔一段时间 ...
- 【Ngui 学习系列之一:简单组件的操作】
一.Buttonunity edit: Sprite作为父对象和背景 -- Collider -- Button script Label 作为子对象和显示文字代码: private UIButton ...
- 《windows核心编程系列》十五谈谈windows线程栈
谈谈windows线程栈. 当系统创建线程时会为线程预订一块地址空间区域,注意仅仅是预订.默认情况下预定的这块区域的大小是1MB,虽然预订这么多,但是系统并不会给全部区域调拨物理存储器.默认情况下,仅 ...
- 《Windows核心编程系列》十二谈谈Windows内存体系结构
Windows内存体系结构 理解Windows内存体系结构是每一个励志成为优秀的Windows程序员所必须的. 进程虚拟地址空间 每个进程都有自己的虚拟地址空间.对于32位操作系统来说,它的地址空间是 ...
- Silverlight for Windows Phone开发系列课程
Silverlight for Windows Phone开发系列课程(1):Windows Phone平台概况 课程简介:本节开始介绍系列课程的概况,包括课程内容,先决条件,学习目的 ...
随机推荐
- XSS Stored 测试
dvwa存储型XSS 存储型XSS:会把用户输入的数据“存储”在服务器端,一般出现在需要用户可以输入数据的地方,比如网站的留言板.评论等地方,当网站这些地方过滤不严格的时候,就会被黑客注入恶意攻击代码 ...
- 系统开发中使用拦截器校验是否登录并使用MD5对用户登录密码进行加密
项目名称:客户管理系统 项目描述: 项目基于javaEE平台,B/S模式开发.使用Struts2.Hibernate/Spring进行项目框架搭建.使用Struts中的Action 控制器进行用户访问 ...
- Python迭代和解析(2):迭代初探
解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html 在Python中支持两种循环格式:while和for.这两种循环的类型不 ...
- 第8章 概述 - Identity Server 4 中文文档(v1.0.0)
快速入门提供了各种常见IdentityServer方案的分步说明.他们从基础到复杂 - 建议您按顺序完成它们. 将IdentityServer添加到ASP.NET Core应用程序 配置Identit ...
- Linq实现左连接、右连接
--一本错误的记录 insert into Book values('错误时怎样练成的',111) --左连接 select s.name,b.name from student as s lef ...
- C# 如何解决 引用的两个同名同版本的DLL冲突
离职后来到现在这家公司,在这几天接到一个项目要求是要通过淘宝聚石塔API来抓取公司的订单流水.按理说这项任务不算很难,但是,你也知道,壮士出征往往死在离出发地不远的地方.现在我们来研究一下为什么会导致 ...
- HAProxy负载均衡技术
软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. HA ...
- Java开发笔记(三十七)利用正则串分割字符串
前面介绍了处理字符串的常用方法,还有一种分割字符串的场景也很常见,也就是按照某个规则将字符串切割为若干子串.分割规则通常是指定某个分隔符,根据字符串内部的分隔符将字符串进行分割,例如逗号.空格等等都可 ...
- Android开发——获得Json数据,并显示图片
流程介绍 使用okhttp网络框架进行get请求,获得json数据 //一个封装好的工具类的静态方法 public static void sendOkHttpRequest(final String ...
- ArcGIS 10.0发布缓存地图服务(详细版)
1.软件准备ArcGIS Destop10.0,ArcGIS Server10.0,Windows系统下自带的IIS6.0以上服务器 1)安装ArcGIS Destop10.0软件,选择完全安装,安装 ...