/// <summary>
        /// 启动服务
         /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ServiceController sc = new ServiceController("WindowsService1");
            if (sc.Status.Equals(ServiceControllerStatus.Stopped))
            {
                sc.Start();
            }
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            ServiceController sc = new ServiceController("MSSQLSERVER");
            if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
            {
                sc.Stop();
            }
        }
        /// <summary>
        /// 安装服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (!isServiceIsExisted("Service1"))
            {                
                string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string serviceFileName = location.Substring(0, location.LastIndexOf('//') + 1) + "WindowsService1.exe";

                InstallmyService(null, serviceFileName);
            }
            else
            {
                MessageBox.Show("系统已经安装了此服务!");
            }
        }
        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (isServiceIsExisted("Service1"))
            {
                string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string serviceFileName = location.Substring(0, location.LastIndexOf('//') + 1) + "WindowsService1.exe";
                UnInstallmyService(serviceFileName);
            }
            else
            {
                MessageBox.Show("系统不存在此服务,不需要卸载!");
            }
        }


        /// <summary>
        /// 检查服务存在的存在性
        /// </summary>
        /// <param name=" NameService ">服务名</param>
        /// <returns>存在返回 true,否则返回 false;</returns>
        public static bool isServiceIsExisted(string NameService)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName.ToLower() == NameService.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 安装Windows服务
        /// </summary>
        /// <param name="stateSaver">集合</param>
        /// <param name="filepath">程序文件路径</param>
        public static void InstallmyService(IDictionary stateSaver, string filepath)
        {
            AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
            AssemblyInstaller1.UseNewContext = true;
            AssemblyInstaller1.Path = filepath;
            AssemblyInstaller1.Install(stateSaver);
            AssemblyInstaller1.Commit(stateSaver);
            AssemblyInstaller1.Dispose();
        }
        /// <summary>
        /// 卸载Windows服务
        /// </summary>
        /// <param name="filepath">程序文件路径</param>
        public static void UnInstallmyService(string filepath)
        {
            AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
            AssemblyInstaller1.UseNewContext = true;
            AssemblyInstaller1.Path = filepath;
            AssemblyInstaller1.Uninstall(null);
            AssemblyInstaller1.Dispose();
        }

用C#代码来安装、卸载、启动、关闭服务的更多相关文章

  1. mariaDB 安装/卸载+启动/关闭 服务

    1.设置环境变量 无论是用户环境变量还是系统环境变量 2.启动服务 进入根目录 名字根据 --install 后的 参数来决定 叫MariaDB,MySQL 都可以 mysqld.exe --inst ...

  2. redis安装 卸载 启动 关闭

    一 redis安装 第一步:在VMware中安装CentOS(参考Linux教程中的安装虚拟机) 第二步:在Linux下安装gcc环境 [root@hadoop ~]#yum install gcc- ...

  3. 【运维技术】VM虚拟机上使用centos7安装docker启动gogs服务教程【含B站视频教程】

    VM虚拟机上使用centos7安装docker启动gogs服务视频教程 BiliBili视频教程链接飞机票,点我 使用VMware Workstation安装Centos7 MinMal系统 第一步: ...

  4. c#创建windows服务(代码方式安装、启动、停止、卸载服务)

    转载于:https://www.cnblogs.com/mq0036/p/7875864.html 一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NE ...

  5. redis 安装及启动关闭

    1.redis下载 方式1:直接去官网下载 https://redis.io/download 方式2:通过命令下载 wget http://download.redis.io/releases/re ...

  6. nginx学习与配置-安装与启动关闭管理

    nginx服务器的安装 安装准备: nginx依赖于pcre库,要先安装pcre yum install pcre pcre-devel cd /usr/local/src/ wget wget ht ...

  7. 批处理启动vm虚拟机服务 vm12启动无界面启动vm虚拟机系统 windows上如何操作服务 sc net启动关闭服务

    windows(win10)批处理脚本 打开vm虚拟机的服务,并且开启无界面虚拟机 @echo off net start "vds" net start "VMAuth ...

  8. 使用Windows命令行启动关闭服务(net,sc用法)

    下面两个命令最好以管理员方式启动cmd窗口,否则出现权限问题. 1.net用于打开没有被禁用的服务, NET命令是功能强大的以命令行方式执行的工具. 它包含了管理网络环境.服务.用户.登陆大部分重要的 ...

  9. ubuntu 下安装和启动SSH 服务

    安装OPENSSH 服务端 sudo apt-get install openssh-server 查看进程是否启动 ps -e | grep ssh 删除密钥文件 rm /etc/ssh/ssh_h ...

  10. BIEE启动关闭服务(转)

    一.环境说明 版本:BIEE11g (BIEE_11.1.1.9.0) OS:CentOS 6.5 64bit (所有的linux服务器都适用) 二.BIEE启动与关闭 BIEE11g 的启动包括三个 ...

随机推荐

  1. 【uoj291】 ZJOI2017—树状数组

    http://uoj.ac/problem/291 (题目链接) 题意 一个写错的树状数组有多大的概率与正常树状数组得出的答案一样. Solution 可以发现这个树状数组维护的是后缀和. 所以二维线 ...

  2. websocket c++ example

    //============================================================================ // Name : websocket.c ...

  3. X11,GTK,QT,GNOME的区别与联系(UI工具总结)

    1,X11是X Window System Protocol, Version 11(RFC1013),是X server和X client之间的通信协议.X server是xfree86/xorg驱 ...

  4. codeblocks下的汇编语言

    Debug->Debugging windows->Disassembly F4+F7 每条语句的调试,查看变量 可以通过两者的比较来互相学习 1.通过查看汇编代码学习c如何改进,减少代码 ...

  5. 超详细从零记录Hadoop2.7.3完全分布式集群部署过程

    超详细从零记录Ubuntu16.04.1 3台服务器上Hadoop2.7.3完全分布式集群部署过程.包含,Ubuntu服务器创建.远程工具连接配置.Ubuntu服务器配置.Hadoop文件配置.Had ...

  6. linu查看当前目录下文件大小

    查看当前目录下文件大小 [root@izm5e33l0ge76tvdj3qtnfz var]# du -sh * .0K adm 190M cache .0K crash 24K db .0K emp ...

  7. 到浏览器顶部的获取js和jquery

    获取当前窗口到页面顶端高度: js: document.documentElement.scrollTop JQ:$(document).scrollTop()或者$(window).scrollTo ...

  8. mysql优化问题汇总

    sql优化-->分区-->分表-->垂直分库-->水平分库-->读写分离 分区 关于分区的博客推荐这个:https://blog.csdn.net/youzhouliu/ ...

  9. jquery 遍历 json【转】

    jquery 遍历 json <HTML> <HEAD> <meta http-equiv="content-Type" content=" ...

  10. lucene教程【转】【补】

    现实流程 lucene 相关jar包 第一个:Lucene-core-4.0.0.jar, 其中包括了常用的文档,索引,搜索,存储等相关核心代码. 第二个:Lucene-analyzers-commo ...