一、通过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>&&(args[].ToLower()=="-install"||args[].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>&&(args[].ToLower()== "-uninstall" || args[].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)==)
{
return true;
}
}
return false;
}
}
}

  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

Windows Service 学习系列(二):C# windows服务:安装、卸载、启动和停止Windows Service几种方式的更多相关文章

  1. 手把手教用C#编写Windows服务 并控制服务 安装、启动、停止、卸载

    Windows服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动, ...

  2. Windows 64下elasticsearch-1.7.1集群 安装、启动、停止

    elasticsearch-1.7.1 (es Windows 64) 安装.启动.停止的详细记录 https://blog.csdn.net/qq_27093465/article/details/ ...

  3. 【转】monkeyrunner学习总结二:连接、安装、启动

    一.获取packageName与startActivity 前置条件:已成功安装配置SL4A并连接PC调试(参见:monkeyrunner学习总结一:环境搭建:http://www.amsoft.cn ...

  4. Windows Service 学习系列(一):建立简单的Windows service

    参考:https://www.cnblogs.com/cncc/p/7170951.html 一.开发环境 操作系统:Windows 7 X64 开发环境:VS2017 编程语言:C# .NET版本: ...

  5. WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

    http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...

  6. RabbitMQ学习系列二-C#代码发送消息

    RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列 http://www.80iter.com/blog/1437455520862503 上一篇已经讲了Rabbi ...

  7. windows服务安装卸载

    到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...

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

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

  9. Windows Server 2012之活动目录域服务的卸载

    Windows Server 2012之活动目录域服务的卸载 2012-07-11 06:27:35 标签:Windows Server 2012 活动目录域服务 卸载 原创作品,允许转载,转载时请务 ...

随机推荐

  1. springboot+mybatis+dubbo+aop日志第三篇

    AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等. Spring AOP模块提供截取拦截应用程序的拦截器,例如,当执行方法时,可以在执行方法之前或之后添加 ...

  2. python学习第一讲,python简介

    目录 python学习第一讲,python简介 一丶python简介 1.解释型语言与编译型语言 2.python的特点 3.python的优缺点 二丶第一个python程序 1.python源程序概 ...

  3. Redis in python

    什么是Redis 数据库类型分为两种,关系型和非关系型,Redis是一个非常重要的非关系型数据库. 既然是数据库,就是存储数据的一个空间,或者说是一个软件,非关系就是不再按照一对一多对多等结构进行外键 ...

  4. .net mvc 导出excel表格

    使用using System.IO; /// /// 导出Excel文件,并自定义文件名 /// public static void DataTableExcel(System.Data.DataT ...

  5. MD5&&DES加密解密帮助类

    public class _MD5_NETCore加密解密 { /// <summary> /// 加密 /// </summary> /// <param name=& ...

  6. Predicate--入门简介

    说明:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素. var predicate = new Predicate&l ...

  7. Java开发笔记(五十二)对象的类型检查

    前面介绍了类的多态性,来自于鸡类的实例chicken,既能用来表达公鸡实例,也能用来表达母鸡实例.可是这导致了一个问题,假如在call方法内部需要手工判断输入参数属于公鸡实例还是母鸡实例,那该如何是好 ...

  8. CentOS7 yum方式安装MySQL5.7

    转载至博客:https://www.cnblogs.com/bigbrotherer/p/7241845.html 在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要 ...

  9. RabbitMQ如何工作和RabbitMQ核心概念

    RabbitMQ是一个开源的消息代理软件.它接受来自生产者的消息并将其传递给消费者.它就像一个中间人,可以用来减少Web应用程序服务器的负载和交付时间. RabbitMQ如何工作 让我们简要介绍一下R ...

  10. 自定义控制台程序导出Dynamics 365实体信息到Excel中。

    本人微信公众号:微软动态CRM专家罗勇 ,回复281或者20181116可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 有时 ...