Bạn có thể sử dụng công cụ Microsoft SC (Sc.exe) để truy vấn trạng thái dịch vụ hoặc thậm chí tạo hoặc xóa một dịch vụ. Dưới đây là một bài viết về việc sử dụng các lệnh này: http://support.microsoft.com/kb/251192
Từ một cửa sổ Command Prompt (nội dung sửa để nhấn mạnh):
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).
Chạy lệnh này để truy vấn một dịch vụ (A) tồn tại và (B) không tồn tại trong kết quả:
(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.
Vì vậy, bạn có thể kiểm tra sự tồn tại của một dịch vụ trước khi cố gắng xóa nó bằng cách sử dụng sau - (tha thứ việc sử dụng thuần phong mỹ tục của CHO tuyên bố, tôi không chắc chắn chính xác làm thế nào để nắm bắt đầu ra của lệnh sc vào một biến hoặc sử dụng nó trong câu lệnh IF) -
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%
Có lẽ tôi đang thiếu một cái gì đó, nhưng sẽ không phải là prebuild cũng không lần đầu tiên (hoặc bất kỳ thời gian ngay sau khi sạch) vì exe chứa các lớp cài đặt chưa được xây dựng chưa? –
thats chính xác, nhưng tôi đã không được xây dựng lại hoặc làm sạch vì vậy nó được tốt. Nhưng bạn đưa ra một điểm tốt. –