Windows Service 学习系列(二):C# windows服务:安装、卸载、启动和停止Windows Service几种方式...

一、通过InstallUtil.exe安装、卸载、启动、停止Windows Service  

  方法一

  1.以管理员身份运行cmd

  2.安装windows服务

      切换cd C:\Windows\Microsoft.NET\Framework\v4.0.30319(InstallUtil.exe的路径下,注意InstallUtil.exe的版本号需要和项目的版本号相同)

  3.安装windows服务

      InstallUtil.exe D:\SimpleService\SimpleService\bin\Debug\SimpleService.exe(项目的路径)

(安装过程中出现的错误:Window Service Install "帐户名无效或不存在,或者密码对于指定的帐户名无效。" 解决方法:填用户名时,要在前面加上 .\)

  4.启动windows服务

      net start Servive1(服务名称) 

  5.卸载windows服务

      InstallUtil.exe /u D:\SimpleService\SimpleService\bin\Debug\SimpleService.exe

 

  方法二  

  1、找到 Installutil.exe 文件,并把它复制到 D:\SimpleService\SimpleService\bin\Debug\目录

  2、现在 Installutil.exe 程序在 D:\SimpleService\SimpleService\bin\Debug 目录下,需要通过cmd命令 "cd" 切换到该目录下。

  3、安装服务:
    installutil.exe SimpleService.exe

  4、卸载服务:
    installutil.exe SimpleService.exe

、通过代码模拟InstallUtil.exe安装、卸载、启动、停止Windows Service

  1、Program.cs中的代码

using System.ServiceProcess;

namespace MyWindowsService
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main(string [] args)
        {
            const string SERVICE_NAME = "MyWindowsService";
            if (args.Length>0&&(args[0].ToLower()=="-install"||args[0].ToLower()=="-i"))
            {
                if (!ServiceIsExisted(SERVICE_NAME))
                {
                    System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { string.Concat(SERVICE_NAME,".exe")});
                    ServiceController c = new ServiceController(SERVICE_NAME);
                    c.Start();
                }
            }
            else if(args.Length>0&&(args[0].ToLower()== "-uninstall" || args[0].ToLower()=="-u"))
            {
                if (ServiceIsExisted(SERVICE_NAME))
                {
                    System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/u", string.Concat(SERVICE_NAME, ".exe")});
                }
            }
            else
            {
                ServiceBase[] ServicesToRun= { new Service1() };                
                ServiceBase.Run(ServicesToRun);
            }            
        }
        /// <summary>
        /// 判断是否了安装该服务
        /// </summary>
        /// <param name="svcName"></param>
        /// <returns></returns>
        private static bool ServiceIsExisted(string svcName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (string.CompareOrdinal(s.ServiceName,svcName)==0)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
View Code

  2、管理员身份打开命令提示符

  3、切换到exe所在的目录,如下图

  4、安装服务

    MyWindowsService  -i

  其中MyWindowsService是服务名称,-i是安装服务的命令符号,可以看一下program.cs的代码就明白了。

 

   5、卸载服务

    MyWindowsService  -i

  其中MyWindowsService是服务名称,-u是卸载服务的命令符号,可以看一下program.cs的代码就明白了。

 

三、通过SC命令安装、卸载、启动、停止Windows Service

  1、安装Windows service

    sc create service1 binPath= "D:\SimpleService\SimpleService\bin\Debug\ SimpleService.exe"

    其中:service1 为创建的服务名,binPath后面是运行exe文件的所在路径

  2、配置服务    

    sc config service1 start= AUTO    (自动)

    sc config service1 start= DEMAND  (手动)

    sc config service1 start= DISABLED(禁用)

    其中service1是创建的服务名

  3、开启服务

    net start service1

    其中service1是创建的服务名

  4、关闭服务    

    net stop service1

    其中service1是创建的服务名

  5、删除服务

    sc delete service1

    其中service1是创建的服务名

四、批处理

(新建一个txt文件,自己命名,把后缀改为.bat文件)

  1、创建、配置、开启服务

@echo.服务启动......
@echo off
@sc create test1 binPath= "C:\Users\Administrator\Desktop\win32srvdemo\win32srvdemo\Debug\win32srvdemo.exe"
@net start test1
@sc config test1 start= AUTO
@echo off
@echo.启动完毕!
@pause

  2、关闭服务

@echo.服务关闭
@echo off
@net stop test1
@echo off
@echo.关闭结束!
@pause

  3、删除服务

@echo.服务删除
@echo off
@sc delete test1
@echo off
@echo.删除结束!
@pause

 

 

 

转载于:https://www.cnblogs.com/qtiger/p/9897517.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值