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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构