1.创建windows服务工程
工程名:ServiceDemo
2.添加加载启动及卸载服务脚本
加载及启动批处理:
@echo off
if exist "%SystemRoot%/Microsoft.NET/Framework/v4.0.30319" goto install
echo Please install .net framework v4.0 first.
pause
goto end
:install
%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil "ServiceDemo.exe"
net start "ServiceDemo.exe"
pause
:end
卸载批处理:
@echo off
if exist "%SystemRoot%/Microsoft.NET/Framework/v4.0.30319" goto uninstall
echo Please install .net framework v4.0 first.
pause
goto end
:uninstall
%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil /uninstall "ServiceDemo.exe"
pause
:end
3.问题来了
服务已装载成功,但未启动成功。
4.分析
从第一张图可以看出服务为Service1,所以将加载脚本改为:
@echo off
if exist "%SystemRoot%/Microsoft.NET/Framework/v4.0.30319" goto install
echo Please install .net framework v4.0 first.
pause
goto end
:install
%SystemRoot%/Microsoft.NET/Framework/v4.0.30319/installutil "ServiceDemo.exe"
net start Service1
pause
:end
成功启动: