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

C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计的更多相关文章

  1. windows Service程序的安装、启动、卸载命令

    安装:%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe ServiceTest.exe 启动:Net Start serv ...

  2. 通过批处理进行Windows服务的安装/卸载&启动/停止

    安装服务 @echo off set checked=2 set PATHS=%~sdp0 echo 按任意键执行安装……? pause>nul if %checked% EQU 2 ( %PA ...

  3. python实现windows Service服务程序

    python实现windows Service服务程序 win32serviceutil.ServiceFramework是封装得很好的Windows服务框架,本文通过继承它来实现. 通过SvcDoR ...

  4. 使用Advanced Installer 13.1打包发布 Windows Service服务程序

    原文: 使用Advanced Installer 13.1打包发布 Windows Service服务程序 项目中需要用到一个定时推送案件状态的需求,本人小菜一只,在同事建议下要写成一个windows ...

  5. Windows下的Nessus安装与启动

    Windows下的Nessus安装与启动 一.安装 在https://www.tenable.com/downloads/nessus下载对应windows版本 双击安装,完成后,访问 https:/ ...

  6. windows service 2008 R2 安装net4.6环境失败,windows service 2008 R2 升级sp1问题

    一.错误 1.因为我的程序是以vs2017开发的,在windows service 2008 R2  IIS部署项目文件报出错误,因此要安装net4.6的环境. 2.windows service 2 ...

  7. redis的安装部署启动停止<17.3.21已更新>

    --------------------------------------------------------- 启动redis时使用下面两条命令: redis-server /etc/redis. ...

  8. 使用Python写Windows Service服务程序

    1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...

  9. mariaDB 安装/卸载+启动/关闭 服务

    1.设置环境变量 无论是用户环境变量还是系统环境变量 2.启动服务 进入根目录 名字根据 --install 后的 参数来决定 叫MariaDB,MySQL 都可以 mysqld.exe --inst ...

随机推荐

  1. JS的document.all函数使用示例

    JS的document.all函数虽然被document.getElement......代替,但是在使用中还是较为常见,下面为大家详细介绍下具体的使用示例: 一: document.all是页面内所 ...

  2. [asp.net core]The requested page cannot be accessed because the related configuration data for the page is invalid.

    bug HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the rela ...

  3. 最新版Xamarin Mono For Android、Monotouch 安装、破解(实时同步更新)

    以上链接如不是最新,以官方为主,官方更新地址如下:http://xamarin.com/installer_assets/v3/Mac/Universal/InstallationManifest.x ...

  4. android面试题总结加强再加强版(一)

    在加强版的基础上又再加强的android应用面试题集 有些补充略显臃肿,只为学习 1.activity的生命周期. 方法 描述 可被杀死 下一个 onCreate() 在activity第一次被创建的 ...

  5. Unity 5.4版本 Application.systemLanguage 失效

    最近在上线双语版本(一个包支持中文.英文二种语言)时,遇到一个坑点 if (ToolUtils.isAndroid()) { if (Application.systemLanguage == Sys ...

  6. redis批量删除key 远程批量删除key

    一.遇到的问题 在开发的过程中,经常会遇到要批量删除某种规则的key,如缓存的课程数据“course-课程uid”,其中课程uid是变量,我们需要删除"course-*"这一类的数 ...

  7. 【原】Linux环境下Shell调用MySQL并实现定时任务

    对于一些周期性事务,我们可以在Linux下,使用shell脚本调用mysql数据库存储过程,并设置定时任务. 本来是要mysql数据库中创建事件任务来,定时执行存储过程,做数据传输的...使用cron ...

  8. linux服务器挂掉自动重启脚本(转)

    实现原理主要是使用linux提供的crontab机制,定时查询服务器进程是否存在,如果宕机则执行我们预设的重启脚本. 首先我们要向crontab加入一个新任务 sudo crontab -e #进入编 ...

  9. 每天一个linux命令(8):rm

    1.命令简介 rm(Remove file 删除目录或文件)删除文件,对于链接文件,只是删除整个链接文件,而原有文件保持不变. 2.用法 rm [选项]... 文件.. 3.选项 -f, –force ...

  10. GDAL指定自定义的金字塔目录

    缘起 对于一般的遥感影像文件,金字塔文件默认都是与影像文件放在同一个目录下,金字塔文件名一般与源影像文件名相同,但后缀名不同.或者金字塔内建于影像内部,但这不是这里所涉及的. 在使用ArcGIS桌面版 ...