Bat file 安装和卸载同级目录下的.net 服务

今天得到个需求 客户需要用batch file 安装和卸载服务,网上搜了一把例子,都只解决了单个问题,我来稍微总结一下

安装服务

@ECHO OFF

REM The following directory is for .NET 4.0 set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319 set PATH=%PATH%;%DOTNETFX4%

echo Installing My Win Service... echo --------------------------------------------------- %DOTNETFX4%\InstallUtil "%~dp0MyService.exe" echo --------------------------------------------------- pause echo Done.

卸载服务

@ECHO OFF

REM The following directory is for .NET 4.0 set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319 set PATH=%PATH%;%DOTNETFX4%

echo UnInstalling My Win Service... echo --------------------------------------------------- %DOTNETFX4%\InstallUtil /u "%~dp0MyService.exe" echo --------------------------------------------------- pause echo Done.

这里%~dp0是关键,目的是找和bat file同一目录下的文件,不然安装会到“C"\windows\system32”下面去找你的服务exe文件

http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work

想写得更好一点可以参看下面网址的solution2

http://www.codeproject.com/Questions/505250/HowplustoplusInstallplusorplusUninstallplusWindows

给同一个 service 用不同的别名安装多次可以参看,(因为网址被国内屏蔽了,所以我把原文给贴了过来 ^_^)

http://journalofasoftwaredev.wordpress.com/2008/07/16/multiple-instances-of-same-windows-service/

Multiple instances of same windows service Filed under: .net, advice, code example, tips — Tags: .net, code example, multiple instances, windows services — Michael Cromwell @ 7:36 pm There are times were you want to be able install multiple instances of the same windows service onto the same server (i.e. to support different environments but you have limited servers at your diposal) this poses a problem, you are probably aware windows will not allow more than 1 windows service to be installed with the same service name and when you create a windows service project and an installer you assign the service name at design time. We need is someway to assign the service name at runtime, well after some extensive googling around I found a way of achieving this, the ProjectInstaller has 2 methods you can override Install and Uninstall which enables us to make changes to the service installer at the at runtime  Yeah that’s great however how do we assign the service name?! Fear not! at our disposal is the Context class that happens to have a Parameters dictionary so we can now capture custom command arguments passed to installutil.exe, enough yapping time for some code:

public override void Install(System.Collections.IDictionary stateSaver)
{
    RetrieveServiceName();
    base.Install(stateSaver);
}
 
public override void Uninstall(System.Collections.IDictionary savedState)
{
    RetrieveServiceName();
    base.Uninstall(savedState);
}
 
private void RetrieveServiceName()
{
    var serviceName = Context.Parameters["servicename"];
    if (!string.IsNullOrEmpty(serviceName))
    {
        this.SomeService.ServiceName = serviceName;
        this.SomeService.DisplayName = serviceName;
    }
}
In this instance I have made the servicename argument optional in which case it will use the default assigned at design time, you could enforce it in your version.

We can now use this like this: installutil /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe and for uninstall: installutil /u /servicename=”My Service [SysTest]” d:\pathToMyService\Service.exe

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

使用MongoDB存储访问者信息

网站的访问者信息的存储一般都是海量的,通常使用关系数据库,现在NoSQL运动火热,满足这样的需求使用NoSQL数据库会更好,网站访问者信息主要是两个功能: 1、...

22010
来自专栏Kotlin入门系列

win7基础 cmd 查看当前已经启动的服务列表

5556
来自专栏GIS讲堂

Highcharts导出图片

Highcharts是在做项目涉及到统计图的时候大家的首选,同时也会用到highcharts的export功能,将统计图导出为图片,刚好,最近也遇到了这样的事情...

5092
来自专栏吴伟祥

Linux命令缩写英文对照记忆(〇) 转

1112
来自专栏bboysoul

如何在metasploit中使用shodan

首先打开metasploit sudo service postgresql start msfconsole

1233
来自专栏landv

c语言_头文件_windows.h

2013
来自专栏C++

Windows核心编程:第7章 线程调度、优先级和关联性

1123
来自专栏技术之路

visual studio 2012 的制作ActiveX、打包和发布

开发环境是Vs 2012  Framework 4.0 源码和制作工具在文章最下边 一. ActiveX控件Demo 新建一个Window窗体控件库项目 ? 在...

1999
来自专栏Hadoop实操

如何在CDH启用Kerberos的情况下安装及使用Sentry(二)

使用beeline连接HiveServer2,创建columnread角色并授权test表s1列的读权限,将columnread角色授权给fayson_r用户组

1.4K8
来自专栏逸鹏说道

★Kali信息收集★8.Nmap :端口扫描

突然发现,微信一次最多推送8篇 参数:(Zenmap是Nmap图形化工具,不想打指令的可以直接使用) 详细:https://nmap.org/man/zh/in...

5454

扫码关注云+社区

领取腾讯云代金券