我想使用Windows命令提示符(而不是Visual Studio命令提示符)安装Windows服务。

我怎么做呢?


当前回答

打开开发人员命令提示符为Admin并导航到

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

现在使用你的。exe所在的路径

InstallUtil "D:\backup\WindowsService\WindowsService1\WindowsService1\obj\Debug\TestService.exe"

其他回答

安装服务:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" 
"C:\Services\myservice.exe"

卸载服务:-

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "C:\Services\myservice.Service.exe"

如果目录名有空格,如c:\program files\abc 123,则必须在路径周围使用双引号。

installutil.exe "c:\program files\abc 123\myservice.exe"

如果你像下面这样设置一个bat文件,事情就会简单得多,

例如,要安装一个服务,创建一个“myserviceinstaller.bat”和“以管理员身份运行”

@echo off
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
installutil.exe "C:\Services\myservice.exe"

if ERRORLEVEL 1 goto error
exit
:error
echo There was a problem
pause

要卸载服务,

只需在installutil命令中添加一个-u。

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "C:\Services\myservice.exe"

您可以使用InstallUtil安装任何windows服务。 1: C:\Windows\Microsoft.NET\Framework64\v4.0.30319在命令提示符下以管理员身份运行。 2:复制Exe路径并输入InstallUtil.exe "your Exe path" 然后按回车键。

如果你想要视觉化。转到下面的链接。 这对我帮助很大。

https://youtu.be/yrdyYxzI7SE

如果你正在使用Powershell并且你想要安装。net服务,你可以使用install - service模块。它是InstalUtil工具的包装器。

它公开了3个命令

Install-Service -调用InstallUtil.exe pathToExecutable命令 Install-ServiceIfNotInstalled -首先检查服务是否存在 如果不执行Install-Service方法,则安装 Uninstall-Service-它卸载服务。可执行文件路径的ServiceName可以使用。

这个模块的代码可以在这里查看

当您的程序集版本和您的Visual studio项目在dot net 2或4上的biild设置安装相同的版本时。

使用installutil安装相同版本的service

如内置点网4个

输入c: \ windows \ microsoft.net \ framework \ v4.0.30319 \ installutil.exe

如内置点网2

输入c: \ windows \ microsoft.net \ framework \ v2.0.11319 \ installutil.exe