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 ...
随机推荐
- 百度地图-修改marker图标(icon)
百度地图-修改marker图标(icon) 学习了:https://blog.csdn.net/clh604/article/details/9412291/ 源码膜拜: var map = new ...
- TP视图命名规则之一
TP视图命名规则之一 如果觉得目录结构太深,可以通过设置 TMPL_FILE_DEPR 参数来配置简化模板的目录层次,例如设置: 'TMPL_FILE_DEPR'=>'_' 默认的模板文件就 ...
- RAID详解[RAID0/RAID1/RAID10/RAID5] (转)
一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘 ...
- windows / Linux 远程桌面访问全面总结 / 共享文件
一般来说,ssh 是指无图形界面形式,是命令行的方式. 速度快. vnc 是的是图形界面形式. 速度慢. ssh 方式登陆: 1.windows ...
- Java 遍历一个对象的属性 将非空属性赋值给另一个对象
//将origin属性注入到destination中 public <T> void mergeObject(T origin, T destination) { if (origin = ...
- ijkPlayer 集成
代码地址如下:http://www.demodashi.com/demo/11957.html 概述 ijkplayer 是一款做视频直播的框架,基于FFmpeg,支持Android和iOS.这里介绍 ...
- 基于环信的仿QQ即时通讯的简单实现
代码地址如下:http://www.demodashi.com/demo/11645.html 我的博客地址 之前一直想实现聊天的功能,但是感觉有点困难,今天看了环信的API,就利用下午的时间动手试了 ...
- Jmeter-性能测试(三)
一.调通脚本(以json串Post接口为例)添加聚合报告(线程组->添加->监听器->聚合报告)并调试好需要压测的脚本,如下已经调通的P_C_B151就是我需要压测的脚本 二.设置场 ...
- centos mysql iptables配置
在CentOS系统中防火墙默认是阻止3306端口的,我们要是想访问mysql数据库,我们需要这个端口,命令如下: iptables -I INPUT -p tcp --dport 3036 -j AC ...
- lucene: IO/FileNotFoundException:(Too many open files) 查询异常解决
http://stackoverflow.com/questions/6210348/too-many-open-files-error-on-lucene baidu zone - 为什么Luc ...