vs.net c# 安装、注册windows service服务,判断服务是否存在,是否启动

一、安装服务:
private void InstallService(IDictionary stateSaver, string filepath)
        {
            try
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName");
                if(!ServiceIsExisted("ServiceName"))
                {
                    //Install Service
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path =filepath;
                    myAssemblyInstaller.Install(stateSaver);
                    myAssemblyInstaller.Commit(stateSaver);
                    myAssemblyInstaller.Dispose();
                    //--Start Service
                    service.Start();
                }
                else
                {
                    if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        service.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("installServiceError\n" + ex.Message);
            }
        }

二、卸载windows服务:
        private void UnInstallService(string filepath)
        {
            try
            {
                if (ServiceIsExisted("ServiceName"))
                {
                    //UnInstall Service
                    AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
                    myAssemblyInstaller.UseNewContext = true;
                    myAssemblyInstaller.Path = filepath;
                    myAssemblyInstaller.Uninstall(null);
                    myAssemblyInstaller.Dispose();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("unInstallServiceError\n" + ex.Message);
            }
        }

三、判断window服务是否存在:
        private bool ServiceIsExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName == serviceName)
                {
                    return true;
                }
            }
            return false;
        }

四、启动服务:
private void StartService(string serviceName)
        {
            if (ServiceIsExisted(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();
                    for (int i = 0; i < 60; i++)
                    {
                        service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                        {
                            break;
                        }
                        if (i == 59)
                        {
                            throw new Exception(startServiceError.Replace("$s$", serviceName));
                        }
                    }
                }
            }
        }

五、停止服务:
        private void StopService(string serviceName)
        {
            if (ServiceIsExisted(serviceName))
            {
                System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    service.Stop();
                    for (int i = 0; i < 60; i++)
                    {
                        service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                        {
                            break;
                        }
                        if (i == 59)
                        {
                            throw new Exception(stopServiceError.Replace("$s$", serviceName));
                        }
                    }
                }
            }
        }

注:手动安装window服务的方法:

在“Visual Studio 2005 命令提示”窗口中,运行:
安装服务:installutil servicepath
卸除服务:installutil /u servicepath

come from http://www.cnblogs.com/hesen/archive/2009/03/13/1411129.html

转载于:https://www.cnblogs.com/wuming/archive/2009/06/18/1505861.html

vs.net c# 安装、注册windows service服务,判断服务是否存在,是否启动相关推荐

  1. windows Service循环任务.服务启动后无法停止重启的解决办法

    protected override void OnStart(string[] args) { while(true) { try { workflow.ProcessWorkFlowMQ(); T ...

  2. python写windows程序_【Python学习】Python 写Windows Service服务程序

    如下遇到自己编写的服务无法启动 需要添加环境变量(标红的) C:\Python27\Scripts;C:\Python27\;C:\Python27\chromedriver.exe;C:\Pytho ...

  3. 自制Windows 7 注册表键值修改服务(Service)

    首先说说为什么要写这么一个服务.由于电脑要在公司域中使用,所以不可避免的会继承域中的组策略配置.域中95% 的计算机是XP系统,部分组策略对于Windows 7 系统来说有些多余而且带来很多麻烦. 问 ...

  4. 配置安装Apache主服务发生错误:(OS 5)拒绝访问。 : AH00369: Failed to open the Windows service manager, perh······

    配置安装Apache主服务发生错误:(OS 5)拒绝访问.  : AH00369: Failed to open the Windows service manager, perhaps you fo ...

  5. 结合MSDN理解windows service 服务安装的三个类。

    servicebase类: 服务类从此类派生,重写方法. 一个可执行文件包含多项服务,每项服务包含单独的安装"注册服务类".服务与日志相关. 可执行文件的main()函数定义那些服 ...

  6. mysql安装提示create_MySQL5.1安装时出现Cannot create windows service for mysql.error:0

    安装MySQL5.1过程中,我把以前MySQL5.0的GUI工具和服务器全部删掉,安装目录全部删掉,数据文件目录名字改掉,注册表用完美卸载清理了. 然后重启安装MySQL5.1(我以前遇到过服务启动不 ...

  7. 新建第一个windows服务(Windows Service)

    首先,请原谅我是一个小白,一直到前段时间才在工作需要的情况下写了第一个windows服务.首先说一下为什么写这个windows服务吧,也就是什么需求要我来写这么一个东西.公司的项目中,需要一个预警功能 ...

  8. python 服务注册_python注册Windows服务

    注册windows服务程序框架 安装pywin32 pip install pywin32 按照下述程序框架编写代码,业务逻辑写main函数里即可. ''' SMWinservice by David ...

  9. mysql5.1安装失败_解决MySQL5.1安装时出现Cannot create windows service for mysql.error:0

    安装MySQL5.1过程中,我把以前MySQL5.0的GUI工具和服务器全部删掉,安装目录全部删掉,数据文件目录名字改掉,注册表用完美卸载清理了. 然后重启安装MySQL5.1(我以前遇到过服务启动不 ...

最新文章

  1. Python 多进程的进程池pool运行时报错:ValueError: Pool not running
  2. windows(xshell)免密码登录
  3. 计算机知识和技能,计算机基本知识和技能PPT课件.ppt
  4. CentOS 5.5-yum安装配置LNMP
  5. python习题集整理汇总
  6. 大屏数据可视化解决方案(上)
  7. DataGuard ORA-01111,ORA-01275文件创建失败问题解决
  8. 3d激光雷达开发(平面映射)
  9. 【JavaScript 笔记 】— 基础语法(数据类型、字符串、数组、对象、Map、Set、iterable、函数基础)
  10. English Through Movie
  11. oracle安装最后一步完成了就消失了_Oracle安装过程中遇到的一些问题及解决方案...
  12. linux自动ping脚本,linux 自动ping ip的shell脚本
  13. 为什么倡导企业使用电子招投标?
  14. 广州地铁公厕(洗手间)和母婴室信息汇总
  15. Hexo NexT 添加背景图片
  16. 重磅推荐!5 款强大的开源报表工具
  17. # IE浏览器打不开网页,google浏览器能打开
  18. The Last Samurai 最后的武士**
  19. 2021.11.17
  20. 手把手教你封杀恶意登录服务器的ip

热门文章

  1. python 声音合成_使用python进行声音生成/合成?
  2. sprintf函数打印数据不对_printf的归宿-数据打印到哪儿了
  3. C标签加EL表达式在jsp页面实现遍历后台内容
  4. 推流中转服务器,视频推流服务器EasyRTMPLive拉转推过程当中遇到复杂目的地址解决方法?...
  5. mysql连接数据了的dep_Mysql-多表连接的操作和用法
  6. 2013二级计算机c语言50,2013年计算机二级C语言课后模拟题五及答案
  7. 仅展示近三天的动态设置_不要把朋友圈设置成3天可见……
  8. 曾经的python爬虫挣钱生活_聊一聊,这些年我用Python爬虫挣钱的那些事
  9. 毛玻璃效果 php,CSS3如何实现磨砂玻璃背景效果
  10. oracle 配置监听和TNS常见的问题