bat批处理脚本命令安装卸载windows服务-InstallUtil.exe和sc命令

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yzy85/article/details/82851445

1、使用Visiual Studio安装正常流程开发Window服务,增加服务安装类,配置服务安装属性,服务名称设置为TestSvr(可自行修改为其他名称),编译,如下:

2、安装.net 框架后,根据服务使用的框架版本,将C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe或C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe复制到服务可执行文件所在文件夹下,如上图。

第一种批处理方法,使用InstallUtil.exe文件安装

3、安装服务,安装.bat,内容如下:其中cd /d %~dp0是定位到当前bat文件所在路径,net start TestSvr中的服务名称为步骤1中设置的服务名称。

@title 安装windows服务
cd /d %~dp0 
InstallUtil.exe WindowsService1.exe 
net start TestSvr
pause 

4、卸载服务,卸载.bat,内容如下:

@title 卸载windows服务
cd /d %~dp0
net stop TestSvr
taskkill /f /im WindowsService1.exe
InstallUtil.exe WindowsService1.exe  -u
pause

第二种bat批处理方法,使用sc命令安装,此时不需要InstallUtil.exe文件

5.安装服务批处理,ServiceInstall.bat,该方法参考地址:https://blog.csdn.net/cctv_end/article/details/51930888

echo ************************************
set str1=%~dp0
set str2=WindowsService1.exe
set str3=ServiceUninstall.bat
set strExe=%str1%%str2%
set strUninstall=%str1%%str3%

echo 清理原有服务项. . .
echo 关闭服务...
net stop TestSvr
echo 卸载服务. . .
sc delete TestSvr
echo 清理完毕,开始安装后台服务. . .

sc create  TestSvr  start= auto binPath= %strExe%
echo 启动服务...
net start TestSvr
echo 操作结束,请在日志 中查看具体的操作结果。
echo *****************************************
pause

6、卸载服务批处理,ServiceUninstall.bat

echo *************************************
echo 关闭服务...
net stop TestSvr
echo 清理原有服务项. . .
sc delete TestSvr
echo 清理完毕. . .
echo *************************************
pause

7、批处理脚本下载:

https://download.csdn.net/download/yzy85/10688976

安装C#服务的批处理脚本-利用InstallUtil.exe文件和sc命令

猜你喜欢

转载自blog.csdn.net/yzy85/article/details/82851445
今日推荐