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服务的更多相关文章

  1. MongoDB的安装启动及做成windows服务

    直接上干货. 官网地址:https://www.mongodb.com/download-center?jmp=nav#community 点击图中链接进入所有版本的下载列表 我下载的是3.6.5版本 ...

  2. c#用控制台程序安装启动停止卸载服务

    第一步:新建控制台项目  第二步:添加服务 第三步:右键新建完成的服务项 点击 在start 和stop事件中分别写上   第四步 编写代码 双击打开 using System; using Syst ...

  3. 使用InstallUtil安装及卸载Windows服务的具体操作 Visual Studio 2012版本

    关于Visual Studio 2012中使用InstallUtil对Windows服务进行安装与卸载的文章,在MSDN中的http://msdn.microsoft.com/en-us/librar ...

  4. 批处理文件安装与卸载Windows服务

    //安装Windows服务 将RECPost.exe和RECPostService替换成自己的项目名称和服务名称,并将文件保存成bat格式.其中%cd%是获取相对路径 @echo off set fi ...

  5. C# 远程服务器 安装、卸载 Windows 服务,读取远程注册表,关闭杀掉远程进程

    这里安装windows服务我们用sc命令,这里需要远程服务器IP,服务名称.显示名称.描述以及执行文件,安装后需要验证服务是否安装成功,验证方法可以直接调用ServiceController来查询服务 ...

  6. 用命令 安装/卸载 windows服务(转)

    第一种方法: 1. 开始 ->运行 ->cmd 2. cd到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(Framework版本号按IIS配置 ...

  7. c# windows server安装启动与卸载

    使用installutil.exe安装卸载服务时,由于需要指向服务的全路径,由于生成目录往往不是服务发布的最终目录,很不便利,下面介绍两种方式方便操作: 方式一: 项目中加入install.bat与u ...

  8. 安装自创建的windows服务。

    安装自创建的windows服务. 使用工具InstallUtil.exe进行安装和卸载创建的windows服务 安装:C:/WINDOWS/Microsoft.NET/Framework/v2.0.5 ...

  9. 【微软版本】redis 安装启动及设置密码<windows>

    redis 安装启动及设置密码<windows>   redis 1. 安装 1.1 下载解压包,直接解压到任意路径下即可 windows下载地址:ttps://github.com/MS ...

  10. 安装和卸载windows服务 bat

    1. 安装 windows服务 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil    [服务路径](例:C:\\test\myt ...

随机推荐

  1. 【Docker】MySQL容器因为内存限制启动失败?

    参考资料: https://github.com/docker-library/mysql/issues/3 Improving MySQL's default configuration:http: ...

  2. jQuery 全选 正反选

    <script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript&quo ...

  3. Wix 安装部署教程 -CustomAction的七种用法

    在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...

  4. 关于Linux开源项目基础组件make编译流程

     关于Linux开源项目基础组件make编译流程 非常多Linux开源项目都会用到编译出可运行文件的make.这个是有一套流程的. 首先,GNU构建系统:https://en.wikipedia. ...

  5. JAVA 的IO操作实例

    实例要求: 1,加法操作: 键盘输入两个数字,完成加法操作.因为从键盘接收过来的内容都是通过字符串形式存放的,所以此时直接通过包装类 Integer将字符串变为基本数据类型. 2,菜单显示: 采用的知 ...

  6. SVN学习(一)——SVN 检出文件步骤、图标显示及含义

    May, I come... 1. 创建一个目录用来存放检出得到的文件,例如MyCRM 2. 直接进入目录MyCRM,点右键 3. 可以看到检出得到的文件 此时文件图标上没有任何标识.可能你会想到通过 ...

  7. Unlink of file 'xx' failed. Should I try again? (y/n) 解决办法

    Unlink of file 'xx' failed. Should I try again? (y/n) 原因:一般遇到这个错输入y/n都不能解决问题,出现这个问题的原因可能是其他程序正在操作git ...

  8. curl Error : maximum redirects followed , 这种问题的一种原因 .

    在stack overflow 上查找到有些网站上需要返回一些cookie的,所以当我们curl当相应的网站时,必须要将返回的cookie保存起来. $cookie = tempnam (" ...

  9. 错误: ISO C++ 不同意在类内初始化很量静态成员

    错误: ISO C++ 不同意在类内初始化很量静态成员      今天開始学C++ primer,在牵扯到Sales_item.h头文件时.出现了一些问题(和C++11新特性相关),当前的编译器版本号 ...

  10. unity, 烘焙lightmap

    1,建一个名为_scene的场景,放一个球体. 2,将球体的Static属性勾选. 3,将默认光源Directional light的Baking属性改为Baked. 4,打开Window->L ...