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 ...
随机推荐
- DirectShow控制台输出和保存视频设备名称
#include "windows.h" #include "TCHAR.h" #include <dshow.h> #include <ve ...
- 浅谈Android移动开发程序员的职业发展之路
现在几乎每个it公司都在开发移动产品,我最早知道Android还是在09年成都某学院上大学的时候,从新闻上知道有这么一家公司,创始人安迪·鲁宾很有名,但安卓到底是做什么的,我并没有关注. 到2010年 ...
- 移动负载均衡技术(MBL)
移动负载均衡技术(MBL) 转至元数据结尾 附件:5 被admin添加,被admin最后更新于四月 27, 2015 转至元数据起始 互联网技术发展到今天,已经进入移动时代,很多在传统CS和BS的 ...
- iOS-贝塞尔曲线之自定义饼图
代码地址如下:http://www.demodashi.com/demo/11981.html 项目中需要统计数据展现, 采用了饼图形式展现. 第一步: 了解下贝塞尔曲线相关概念 贝塞尔曲线相关概念: ...
- Android_View_得到Activity
Context context = view.getContext(); if (context instanceof Activity){ Activity activity = (Activity ...
- Oracle undo 表空间管理 (摘DAVID)
Oracle 的Undo有两种方式: 一是使用undo 表空间,二是使用回滚段. 我们通过 undo_management 参数来控制使用哪种方式,如果设为auto,就使用UNDO 表空间,这时必须要 ...
- useradd命令
◆useradd 1.作用 useradd命令用来建立用户帐号和创建用户的起始目录,使用权限是超级用户. 2.格式 useradd [-d home] [-s shell] [-c comment] ...
- Redis(四):常用数据类型和命令
命令手册网址 http://doc.redisfans.com/ Redis数据类型 l String l Hash l List l Set l Sorted Set Redis中还有3种特殊的数据 ...
- docker的使用02
自定义容器名称: docker run --name -i -t ubuntu /bin/bash docker ps -a 数据卷的使用 数据卷其实就是容器和宿主机目录之间的映射. 具体实现: su ...
- 使用pycharm手动搭建python语言django开发环境(五) 使用日志模块打日志
1.在项目的settings.py中增加日志相关声明 #增加日志设置 LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'fil ...