C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计
关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出错
那么如果你只是用一次就不用了那么命令行业无所谓
关于Windows Service程序的创建可以参考上一篇
C#Windows Service程序的创建安装与卸载
一、命令行安装与卸载
安装服务:
installutil.exe filename
卸载服务:
installutil.exe /u filename
安装服务程序
因为Installutil.exe程序在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 目录下,需要通过cmd命令 "cd" 切换目录。v4.0.30319是编译该Windows Service程序的版本(自己选择对应的版本)
二、开发环境
操作系统:Windows7x64 sp1 专业版
开发环境:Visual studio 2013
编程语言:C#
.NET版本: .NET Frmework 4.0
三、新建一个客户端程序进行安装/卸载、启动/停止
1.新建一个WinForm窗体应用程序起名为Windows Service Client
2.添加四个按钮分别为安装服务/卸载服务、启动服务/停止服务
3.引入俩个命名空间引用“System.ServiceProcess”及“System.Configuration.Install”
4.编写代码如下
string serviceFilePath = Environment.CurrentDirectory + "\\WindowsServiceDemo.exe";
//string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe";
string serviceName = "ServiceDemo"; public Form1()
{
InitializeComponent();
} /// <summary>
/// 安装服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.UninstallService(serviceName);
}
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.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
} /// <summary>
/// 启动服务程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStart(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);
}
} /// <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();
}
}
}
5.引入WindowsServerDemo程序到本项目中便于安装等
6.由于需要安装服务,故需要使用UAC中Administrator的权限,鼠标右击项目“WindowsServiceClient”,在弹出的上下文菜单中选择“添加”->“新建项”,在弹出的选择窗体中选择“应用程序清单文件”并单击确定,如下图所示:
7.打开该文件,并将<requestedExecutionLevel level="asInvoker" uiAccess="false" />改为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<!--修改前
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
-->
<!--修改后-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
8.启动程序记住(Visual studio 2013也得用管理员身份运行),顺便开启计算机->管理-->服务来查看
分别单击测试安装服务,启动服务,停止服务,卸载服务,分别查看服务列表
服务列表
源代码工程文件下载
参考博客:https://www.cnblogs.com/mq0036/p/7875864.html