C#安装,启动,停止,卸载Windows服务
public class OptionServices
{
//安装服务
public static void InstallService(string filepath, string serviceName, string[] options)
{
try
{
if (!IsServiceExisted(serviceName))
{
IDictionary mySavedState = new Hashtable();
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.CommandLine = options;
myAssemblyInstaller.Install(mySavedState);
myAssemblyInstaller.Commit(mySavedState);
myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("Install Service Error\n" + ex.Message);
}
}
//卸载服务
public static void UnInstallService(string filepath, string serviceName, string[] options)
{
try
{
if (IsServiceExisted(serviceName))
{
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.CommandLine = options;
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("UnInstall Service Error\n" + ex.Message);
}
}
//判断服务是否存在
public static bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
//启动服务
public static void StartService(string serviceName)
{
if (IsServiceExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running &&
service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(60));
}
}
}
//停止服务
public static void StopService(string serviceName)
{
if (IsServiceExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
}
}
}
}
C#安装,启动,停止,卸载Windows服务的更多相关文章
- MongoDB的安装启动及做成windows服务
直接上干货. 官网地址:https://www.mongodb.com/download-center?jmp=nav#community 点击图中链接进入所有版本的下载列表 我下载的是3.6.5版本 ...
- c#用控制台程序安装启动停止卸载服务
第一步:新建控制台项目 第二步:添加服务 第三步:右键新建完成的服务项 点击 在start 和stop事件中分别写上 第四步 编写代码 双击打开 using System; using Syst ...
- 使用InstallUtil安装及卸载Windows服务的具体操作 Visual Studio 2012版本
关于Visual Studio 2012中使用InstallUtil对Windows服务进行安装与卸载的文章,在MSDN中的http://msdn.microsoft.com/en-us/librar ...
- 批处理文件安装与卸载Windows服务
//安装Windows服务 将RECPost.exe和RECPostService替换成自己的项目名称和服务名称,并将文件保存成bat格式.其中%cd%是获取相对路径 @echo off set fi ...
- C# 远程服务器 安装、卸载 Windows 服务,读取远程注册表,关闭杀掉远程进程
这里安装windows服务我们用sc命令,这里需要远程服务器IP,服务名称.显示名称.描述以及执行文件,安装后需要验证服务是否安装成功,验证方法可以直接调用ServiceController来查询服务 ...
- 用命令 安装/卸载 windows服务(转)
第一种方法: 1. 开始 ->运行 ->cmd 2. cd到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(Framework版本号按IIS配置 ...
- c# windows server安装启动与卸载
使用installutil.exe安装卸载服务时,由于需要指向服务的全路径,由于生成目录往往不是服务发布的最终目录,很不便利,下面介绍两种方式方便操作: 方式一: 项目中加入install.bat与u ...
- 安装自创建的windows服务。
安装自创建的windows服务. 使用工具InstallUtil.exe进行安装和卸载创建的windows服务 安装:C:/WINDOWS/Microsoft.NET/Framework/v2.0.5 ...
- 【微软版本】redis 安装启动及设置密码<windows>
redis 安装启动及设置密码<windows> redis 1. 安装 1.1 下载解压包,直接解压到任意路径下即可 windows下载地址:ttps://github.com/MS ...
- 安装和卸载windows服务 bat
1. 安装 windows服务 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil [服务路径](例:C:\\test\myt ...
随机推荐
- Docker解析及轻量级PaaS平台演练(四)--Fig相关介绍
本篇中将会使用开源工具Fig Fig是什么? 简单的说就是对Docker的封装,从而方便我们构建应用的运行环境 它所做的事情是协调Docker上的各个Container之间的联系,并通过服务发现的方式 ...
- linux设置定制器自动执行任务
基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 ...
- Gitlab安装部署及基础操作
环境说明 系统版本 CentOS 7.2 x86_64(较新版本的gitlab集成了更多功能,顺利运行起来的硬件要求较高,这里给了3G内存) 软件版本 gitlab-ce-10.8.4 GitLa ...
- angular 学习理解笔记
原文:https://github.com/eoinkelly/notes/blob/master/angular/book-building-web-apps-w-angular/angular.m ...
- 安装dubbo-admin报错 URIType BeanCreationException
安装dubbo-admin报错 URIType BeanCreationException 学习了:https://blog.csdn.net/lsm135/article/details/52725 ...
- Servlet——简单用户登录实例+http协议解析
编写项目.用户登录系统1.0版本号 登录界面Servlet: package com.gavin.view; import java.io.IOException; import java.io.Pr ...
- python abstractmethod 对象比较
from functools import total_ordering from abc import ABCMeta,abstractmethod @total_ordering class Sh ...
- lodash escapeRegExp 转义正则特殊字符
_.escapeRegExp([string='']) 转义RegExp 中特殊的字符 "^", "$", "\", ".&quo ...
- asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(五)—— 解决tabs选择已建tab显示但datagrid的toolbar消失的问题
项目需要反复运行,调整bug.发现在选择已有选项卡时,虽然不需要再新建tab,直接跳转到已有的tab上,但问题是显示的datagrid有事会出现toolbar消失的问题.网上也有不少同学出现类似问题, ...
- flink-connector-kafka consumer checkpoint源码分析
转发请注明原创地址:http://www.cnblogs.com/dongxiao-yang/p/7700600.html <flink-connector-kafka consumer的top ...