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>&&(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几种方式的更多相关文章
- 手把手教用C#编写Windows服务 并控制服务 安装、启动、停止、卸载
Windows服务 Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动, ...
- Windows 64下elasticsearch-1.7.1集群 安装、启动、停止
elasticsearch-1.7.1 (es Windows 64) 安装.启动.停止的详细记录 https://blog.csdn.net/qq_27093465/article/details/ ...
- 【转】monkeyrunner学习总结二:连接、安装、启动
一.获取packageName与startActivity 前置条件:已成功安装配置SL4A并连接PC调试(参见:monkeyrunner学习总结一:环境搭建:http://www.amsoft.cn ...
- Windows Service 学习系列(一):建立简单的Windows service
参考:https://www.cnblogs.com/cncc/p/7170951.html 一.开发环境 操作系统:Windows 7 X64 开发环境:VS2017 编程语言:C# .NET版本: ...
- WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】
http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF ...
- RabbitMQ学习系列二-C#代码发送消息
RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列 http://www.80iter.com/blog/1437455520862503 上一篇已经讲了Rabbi ...
- windows服务安装卸载
到C盘下找到对应的开发VS的installutil.exe文件,复制到程序的执行文件(*.exe)相同目录下在开始程序中找到VS命令提示工具 转到程序的执行文件(*.exe)目录下 C:\>cd ...
- c#创建windows服务(代码方式安装、启动、停止、卸载服务)
转载于:https://www.cnblogs.com/mq0036/p/7875864.html 一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NE ...
- Windows Server 2012之活动目录域服务的卸载
Windows Server 2012之活动目录域服务的卸载 2012-07-11 06:27:35 标签:Windows Server 2012 活动目录域服务 卸载 原创作品,允许转载,转载时请务 ...
随机推荐
- redis 系列26 Cluster高可用 (1)
一.概述 Redis集群提供了分布式数据库方案,集群通过分片来进行数据共享,并提供复制和故障转移功能.在大数据量方面的高可用方案,cluster集群比Sentinel有优势.但Redis集群并不支持处 ...
- Python爬虫入门教程 7-100 蜂鸟网图片爬取之二
蜂鸟网图片--简介 今天玩点新鲜的,使用一个新库 aiohttp ,利用它提高咱爬虫的爬取速度. 安装模块常规套路 pip install aiohttp 运行之后等待,安装完毕,想要深造,那么官方文 ...
- git - 管理项目(SourceTree的使用)
Git 相关命令操作全 1.SourceTree 是什么? SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,拥有可视化界面,容易上手操作.同时它也是M ...
- 为啥用ip不可以访问知乎,而百度却可以?
我们先来ping知乎的域名,然后可以得到响应的服务器的ip 之后我们用浏览器来访问这个ip,结果如下 被拒绝访问了. 而用ip来访问百度,则没啥问题,如图 访问知乎的时候,域名可以访问,ip不可以访问 ...
- 大战Java虚拟机【2】—— GC策略
前言 前面我们已经知道了Java虚拟机所做的事情就是回收那些不用的垃圾,那些不用的对象.那么问题来了,我们如何知道一个对象我们不需要使用了呢?程序在使用的过程中会不断的创建对象,这些所创建的对象指不定 ...
- HBase学习——4.HBase过滤器
1.过滤器 基础API中的查询操作在面对大量数据的时候是非常苍白的,这里Hbase提供了高级的查询方法:Filter.Filter可以根据簇.列.版本等更多的条件来对数据进行过滤,基于Hbase本身提 ...
- leetcode — pascals-triangle-ii
import java.util.Arrays; /** * * Source : https://oj.leetcode.com/problems/pascals-triangle-ii/ * * ...
- 基于hashicorp/raft的分布式一致性实战教学
本文由云+社区发表 作者:Super 导语:hashicorp/raft是raft算法的一种比较流行的golang实现,基于它能够比较方便的构建具有强一致性的分布式系统.本文通过实现一个简单的分布式缓 ...
- 图解ARP协议(三)ARP防御篇-如何揪出“内鬼”并“优雅的还手”
一.ARP防御概述 通过之前的文章,我们已经了解了ARP攻击的危害,黑客采用ARP软件进行扫描并发送欺骗应答,同处一个局域网的普通用户就可能遭受断网攻击.流量被限.账号被窃的危险.由于攻击门槛非常低, ...
- AppBoxFuture(一): Hello Future!
AppBoxFuture是一个快速应用框架(Rapid Application Framework),是作者十几年从事信息化建设的经验结晶.框架具备以下一些特色: 极简的分布式系统架构 根据需要可 ...