Wypróbuj własny wiersz polecenia zdarzenia po kompilacji programu Visual Studio .
Spróbuj dodać to w post-build:
@echo off
sc query "ServiceName" > nul
if errorlevel 1060 goto install
goto stop
:delete
echo delete
sc delete "ServiceName" > nul
echo %errorlevel%
goto install
:install
echo install
sc create "ServiceName" displayname= "Service Display Name" binpath= "$(TargetPath)" start= auto > nul
echo %errorlevel%
goto start
:start
echo start
sc start "ServiceName" > nul
echo %errorlevel%
goto end
:stop
echo stop
sc stop "ServiceName" > nul
echo %errorlevel%
goto delete
:end
W przypadku błędu kompilacji z takim komunikatem Error 1 The command "@echo off sc query "ServiceName" > nul
, Ctrl+ Cnastępnie Ctrl+ Vkomunikat o błędzie do Notatnika i spójrz na ostatnie zdanie wiadomości.
To mogłoby mówić exited with code x
. Poszukaj kodu w typowym błędzie tutaj i zobacz, jak go rozwiązać.
1072 -- Marked for deletion → Close all applications that maybe using the service including services.msc and Windows event log.
1058 -- Can't be started because disabled or has no enabled associated devices → just delete it.
1060 -- Doesn't exist → just delete it.
1062 -- Has not been started → just delete it.
1053 -- Didn't respond to start or control → see event log (if logged to event log). It may be the service itself throwing an exception.
1056 -- Service is already running → stop the service, and then delete.
Więcej o kodach błędów tutaj .
A jeśli błąd kompilacji z takim komunikatem,
Error 11 Could not copy "obj\x86\Debug\ServiceName.exe" to "bin\Debug\ServiceName.exe". Exceeded retry count of 10. Failed. ServiceName
Error 12 Unable to copy file "obj\x86\Debug\ServiceName.exe" to "bin\Debug\ServiceName.exe". The process cannot access the file 'bin\Debug\ServiceName.exe' because it is being used by another process. ServiceName
otwórz cmd, a następnie spróbuj najpierw go zabić taskkill /fi "services eq ServiceName" /f
Jeśli wszystko jest w porządku, F5powinno wystarczyć do debugowania.