.net – 使用InstallUtil卸载不存在的服务

前端之家收集整理的这篇文章主要介绍了.net – 使用InstallUtil卸载不存在的服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用我的服务的pre和post构建事件来卸载和安装服务.唯一的问题是,第一次另一个开发人员使用预构建事件时它失败了,因为尚未安装该服务.

我卸载的当前预构建事件是

  1. %WinDir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /u $(TargetPath)

如何在服务已经安装时才​​使用它来卸载?

您可以使用Microsoft SC工具(Sc.exe)查询服务的状态,甚至可以创建或删除服务.这是一篇关于使用此命令的文章http://support.microsoft.com/kb/251192

从命令提示符窗口(为强调编辑的内容):

  1. C:\windows\system32>sc
  2. DESCRIPTION:
  3. SC is a command line program used for communicating with the
  4. Service Control Manager and services.
  5. USAGE:
  6. sc <server> [command] [service name] <option1> <option2>...
  7.  
  8. The option <server> has the form "\\ServerName"
  9. Further help on commands can be obtained by typing: "sc [command]"
  10. Commands:
  11. query-----------Queries the status for a service,or
  12. enumerates the status for types of services.
  13. queryex---------Queries the extended status for a service,or
  14. enumerates the status for types of services.
  15. start-----------Starts a service.
  16. pause-----------Sends a PAUSE control request to a service.
  17. continue--------Sends a CONTINUE control request to a service.
  18. stop------------Sends a STOP request to a service.
  19. delete----------Deletes a service (from the registry).
  20. create----------Creates a service. (adds it to the registry).

运行此命令以查询(A)存在且(B)不存在的服务导致:

(一个)

  1. C:\Windows\System32>sc query W32Time
  2.  
  3. SERVICE_NAME: W32Time
  4. TYPE : 20 WIN32_SHARE_PROCESS
  5. STATE : 1 STOPPED
  6. WIN32_EXIT_CODE : 1077 (0x435)
  7. SERVICE_EXIT_CODE : 0 (0x0)
  8. CHECKPOINT : 0x0
  9. WAIT_HINT : 0x0

(B)

  1. C:\Windows\System32>sc query nothere
  2. [SC] EnumQueryServicesStatus:OpenService Failed 1060:
  3.  
  4. The specified service does not exist as an installed service.

因此,您可以在尝试使用以下内容删除它之前测试服务的存在 – (原谅令人厌恶地使用FOR语句,我不确定如何将sc命令的输出捕获到变量或在IF声明中使用它) –

  1. set svcname=W32Time
  2. set svc=exists
  3. for /f "delims=" %%o in ('sc query %svcname% ^| find "FAIL"') do set svc=notexists
  4.  
  5. if "%svc%"=="exists" sc delete %svcname%

猜你在找的Windows相关文章