I'm using pre and post build events of my service to uninstall and install the service. The only problem is that the first time another developer uses the pre build event it fails since the service is not yet installed.
My current pre build event wh开发者_如何转开发ich uninstalls is
%WinDir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /u $(TargetPath)
How can i use this to ONLY uninstall when the service already installed?
You can use the Microsoft SC tool (Sc.exe) to query the status of a service or even create or delete one. Here is an article on the use of this command: http://support.microsoft.com/kb/251192
From a Command Prompt window (content edited for emphasis):
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).
Running this command to query a service that (A) exists and (B) does not exist results in:
(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.
So, you could test for the existence of a service before attempting to delete it using the following -- (pardon the repulsive use of the FOR statement, I'm not exactly sure how to capture the output of the sc command into a variable or use it in an IF statement) --
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%
I am using Visual Studio 2008 to build a service and, like you, I want it to reinstall the service for me when I have to rebuild it.
My prebuild is
net stop P2PSN.Bridge.Service
$(FrameworkDir)\installutil.exe /u $(TargetPath)
Exit /b 0
My post build is
$(FrameworkDir)\installutil.exe /ShowCallStack $(TargetPath)
net start P2PSN.Bridge.Service
The "Exit /b 0" means that, even if the service is not installed it will not fail. Similarly if you haven't built the service yet, it will still work. After the first time both of these commands will obviously succeed.
To get this to work you MUST RUN VISUAL STUDIO with ELEVATED PERMISSIONS.
Hope this helps.
To build on Jim Grimmett's solution and use it with .net 4.0 the pre build becomes:
net stop yourServiceName
%WinDir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /u "$(TargetPath)"
Exit /b 0
and for the post build:
%WinDir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /i "$(TargetPath)"
net start yourServiceName
rem Exit /b 0
Sadly the $(FrameworkDir) did not expand into the .net 4.0 path
精彩评论