前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Bat file 安装和卸载同级目录下的.net 服务

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

作者头像
麦克-堂
发布2018-04-12 16:22:45
8310
发布2018-04-12 16:22:45
举报

今天得到个需求 客户需要用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:

代码语言:java
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-04-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档