一、通過InstallUtil.exe安裝、卸載、啟動、停止Windows Service
方法一
1.以管理員身份運行cmd
2.安裝windows服務
切換cd C:\Windows\Microsoft.NET\Framework\v4.0.30319(InstallUtil.exe的路徑下,注意InstallUtil.exe的版本號需要和項目的版本號相同)
3.安裝windows服務
InstallUtil.exe D:\SimpleService\SimpleService\bin\Debug\SimpleService.exe(項目的路徑)
(安裝過程中出現的錯誤:Window Service Install "帳戶名無效或不存在,或者密碼對於指定的帳戶名無效。" 解決方法:填用戶名時,要在前面加上 .\)
4.啟動windows服務
net start Servive1(服務名稱)
5.卸載windows服務
InstallUtil.exe /u D:\SimpleService\SimpleService\bin\Debug\SimpleService.exe
方法二
1、找到 Installutil.exe 文件,並把它復制到 D:\SimpleService\SimpleService\bin\Debug\目錄
2、現在 Installutil.exe 程序在 D:\SimpleService\SimpleService\bin\Debug 目錄下,需要通過cmd命令 "cd" 切換到該目錄下。
3、安裝服務:
installutil.exe SimpleService.exe
4、卸載服務:
installutil.exe SimpleService.exe
二、通過代碼模擬InstallUtil.exe安裝、卸載、啟動、停止Windows Service
1、Program.cs中的代碼

using System.ServiceProcess; namespace MyWindowsService { static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> static void Main(string [] args) { const string SERVICE_NAME = "MyWindowsService"; if (args.Length>0&&(args[0].ToLower()=="-install"||args[0].ToLower()=="-i")) { if (!ServiceIsExisted(SERVICE_NAME)) { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { string.Concat(SERVICE_NAME,".exe")}); ServiceController c = new ServiceController(SERVICE_NAME); c.Start(); } } else if(args.Length>0&&(args[0].ToLower()== "-uninstall" || args[0].ToLower()=="-u")) { if (ServiceIsExisted(SERVICE_NAME)) { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/u", string.Concat(SERVICE_NAME, ".exe")}); } } else { ServiceBase[] ServicesToRun= { new Service1() }; ServiceBase.Run(ServicesToRun); } } /// <summary> /// 判斷是否了安裝該服務 /// </summary> /// <param name="svcName"></param> /// <returns></returns> private static bool ServiceIsExisted(string svcName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (string.CompareOrdinal(s.ServiceName,svcName)==0) { return true; } } return false; } } }
2、管理員身份打開命令提示符
3、切換到exe所在的目錄,如下圖
4、安裝服務
MyWindowsService -i
其中MyWindowsService是服務名稱,-i是安裝服務的命令符號,可以看一下program.cs的代碼就明白了。
5、卸載服務
MyWindowsService -i
其中MyWindowsService是服務名稱,-u是卸載服務的命令符號,可以看一下program.cs的代碼就明白了。
三、通過SC命令安裝、卸載、啟動、停止Windows Service
1、安裝Windows service
sc create service1 binPath= "D:\SimpleService\SimpleService\bin\Debug\ SimpleService.exe"
其中:service1 為創建的服務名,binPath后面是運行exe文件的所在路徑
2、配置服務
sc config service1 start= AUTO (自動)
sc config service1 start= DEMAND (手動)
sc config service1 start= DISABLED(禁用)
其中service1是創建的服務名
3、開啟服務
net start service1
其中service1是創建的服務名
4、關閉服務
net stop service1
其中service1是創建的服務名
5、刪除服務
sc delete service1
其中service1是創建的服務名
四、批處理
(新建一個txt文件,自己命名,把后綴改為.bat文件)
1、創建、配置、開啟服務
@echo.服務啟動...... @echo off @sc create test1 binPath= "C:\Users\Administrator\Desktop\win32srvdemo\win32srvdemo\Debug\win32srvdemo.exe" @net start test1 @sc config test1 start= AUTO @echo off @echo.啟動完畢! @pause
2、關閉服務
@echo.服務關閉
@echo off
@net stop test1
@echo off
@echo.關閉結束!
@pause
3、刪除服務
@echo.服務刪除
@echo off
@sc delete test1
@echo off
@echo.刪除結束!
@pause