我使用服务的预构建事件和post构建事件卸载和安装服务。唯一的问题是,当另一个开发人员第一次使用预构建事件时,它会失败,因为服务尚未安装。
我当前卸载的预构建事件是
%WinDir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /u $(TargetPath)当服务已经安装时,我如何使用它只用于卸载?
发布于 2010-10-26 01:10:56
您可以使用Microsoft工具(Sc.exe)查询服务的状态,甚至可以创建或删除服务。下面是一篇关于使用此命令的文章:http://support.microsoft.com/kb/251192
从命令提示符窗口(为强调而编辑的内容):
C:\windows\system32>sc
DESCRIPTION:
SC is a command line program used for communicating with the
Service Control Manager and services.
USAGE:
sc <server> [command] [service name] <option1> <option2>...
The option <server> has the form "\\ServerName"
Further help on commands can be obtained by typing: "sc [command]"
Commands:
query-----------Queries the status for a service, or
enumerates the status for types of services.
queryex---------Queries the extended status for a service, or
enumerates the status for types of services.
start-----------Starts a service.
pause-----------Sends a PAUSE control request to a service.
continue--------Sends a CONTINUE control request to a service.
stop------------Sends a STOP request to a service.
delete----------Deletes a service (from the registry).
create----------Creates a service. (adds it to the registry).运行此命令查询(A)存在且(B)不存在的服务将导致:
(A)
C:\Windows\System32>sc query W32Time
SERVICE_NAME: W32Time
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 1 STOPPED
WIN32_EXIT_CODE : 1077 (0x435)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0(B)
C:\Windows\System32>sc query nothere
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:
The specified service does not exist as an installed service.因此,在尝试使用以下方法删除服务之前,您可以测试服务是否存在
set svcname=W32Time
set svc=exists
for /f "delims=" %%o in ('sc query %svcname% ^| find "FAIL"') do set svc=notexists
if "%svc%"=="exists" sc delete %svcname%发布于 2011-12-21 16:20:20
我正在使用Visual 2008构建一个服务,并且,和您一样,我希望它在我需要重新构建服务时为我重新安装它。
我的预建是
net stop P2PSN.Bridge.Service
$(FrameworkDir)\installutil.exe /u $(TargetPath)
Exit /b 0我的职位建设是
$(FrameworkDir)\installutil.exe /ShowCallStack $(TargetPath)
net start P2PSN.Bridge.Service“退出/b 0”意味着,即使没有安装服务,它也不会失败。类似地,如果您还没有构建该服务,它仍将工作。在第一次之后,这两个命令显然都会成功。
若要使此操作正常,必须使用提升的权限运行VISUAL STUDIO。
希望这能有所帮助。
发布于 2013-02-05 17:01:34
要构建的解决方案并将其与.net 4.0一起使用,预构建如下:
净停止yourServiceName
%WinDir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /u "$(TargetPath)“
出口/b 0
至于岗位建设:
%WinDir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /i "$(TargetPath)“
净启动yourServiceName
快速rem出口/b 0
遗憾的是,$(FrameworkDir)没有扩展到.net 4.0路径中
https://stackoverflow.com/questions/4019795
复制相似问题