-
卸載很簡單,打開cmd, 直接輸入 sc delete WinServiceTest即可
其餘安裝方式:
裝Winfows服務首先要添加安裝程序,添加安裝程序步驟以下:
一、將Windows服務程序切換到設計視圖, 右擊設計視圖選擇「添加安裝程序」
二、切換到剛被添加的ProjectInstaller的設計視圖
通常設置以下:
設置serviceInstaller1組件的屬性: 1) ServiceName = 服務名稱 2) StartType = Automatic ,即自動 設置serviceProcessInstaller1組件的屬性 1) Account = LocalSystem,帳戶通常設置爲本地系統
三、生成解決方案
安裝服務:
方法1、使用DOS命令安裝window服務
一、在服務所在的文件夾下的bin\debug文件夾下找到.exe文件(例如WindowsService1.exe)
將此文件拷貝到你想安裝的文件夾中。
二、進入DOS界面
(VS2008-->Visual Studio Tools-->Visual Studio 2008 命令提示)來進入DOS,直接用cmd可能有些命令找不到;
三、輸入
方法2、使用安裝項目安裝windows服務
我的比較推薦這個方法,選擇目錄安裝更靈活,並且不用在DOS環境下運行。
由於本人比較懶,直接給出別人總結的地址
http://blog.csdn.net/dyzcode/article/details/6981547
注意,之後每次服務項目有更改的時候,須要編譯服務後,在安裝項目中刷新依賴項!!!
方法3、
在ProjectInstaller.cs的後臺代碼中添加安裝服務和卸載服務的代碼
- /// <summary>
- /// 安裝服務
- /// </summary>
- /// <param name="stateSaver"></param>
- public override void Install(System.Collections.IDictionary stateSaver)
- {
- Microsoft.Win32.RegistryKey system,
- //HKEY_LOCAL_MACHINE\Services\CurrentControlSet
- currentControlSet,
- //...\Services
- services,
- //...\<Service Name>
- service,
- //...\Parameters - this is where you can put service-specific configuration
- config;
- try
- {
- //Let the project installer do its job
- base.Install(stateSaver);
- //Open the HKEY_LOCAL_MACHINE\SYSTEM key
- system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
- //Open CurrentControlSet
- currentControlSet = system.OpenSubKey("CurrentControlSet");
- //Go to the services key
- services = currentControlSet.OpenSubKey("Services");
- //Open the key for your service, and allow writing
- service = services.OpenSubKey(conServiceName, true);
- //Add your service's description as a REG_SZ value named "Description"
- service.SetValue("Description", "<span style="font-family: KaiTi_GB2312;">描述語言</span>");
- //(Optional) Add some custom information your service will use...
- config = service.CreateSubKey("Parameters");
- }
- catch (Exception e)
- {
- Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
- }
- }
- /// <summary>
- /// 卸載服務
- /// </summary>
- /// <param name="savedState"></param>
- public override void Uninstall(System.Collections.IDictionary savedState)
- {
- Microsoft.Win32.RegistryKey system,
- currentControlSet,
- services,
- service;
- try
- {
- //Drill down to the service key and open it with write permission
- system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
- currentControlSet = system.OpenSubKey("CurrentControlSet");
- services = currentControlSet.OpenSubKey("Services");
- service = services.OpenSubKey(conServiceName, true);
- //Delete any keys you created during installation (or that your service created)
- service.DeleteSubKeyTree("Parameters");
- //...
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
- }
- finally
- {
- //Let the project installer do its job
- base.Uninstall(savedState);
- }
- }
/// <summary> /// 安裝服務 /// </summary> /// <param name="stateSaver"></param> public override void Install(System.Collections.IDictionary stateSaver) { Microsoft.Win32.RegistryKey system, //HKEY_LOCAL_MACHINE\Services\CurrentControlSet currentControlSet, //...\Services services, //...\<Service Name> service, //...\Parameters - this is where you can put service-specific configuration config; try { //Let the project installer do its job base.Install(stateSaver); //Open the HKEY_LOCAL_MACHINE\SYSTEM key system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System"); //Open CurrentControlSet currentControlSet = system.OpenSubKey("CurrentControlSet"); //Go to the services key services = currentControlSet.OpenSubKey("Services"); //Open the key for your service, and allow writing service = services.OpenSubKey(conServiceName, true); //Add your service's description as a REG_SZ value named "Description" service.SetValue("Description", "描述語言"); //(Optional) Add some custom information your service will use... config = service.CreateSubKey("Parameters"); } catch (Exception e) { Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString()); } } /// <summary> /// 卸載服務 /// </summary> /// <param name="savedState"></param> public override void Uninstall(System.Collections.IDictionary savedState) { Microsoft.Win32.RegistryKey system, currentControlSet, services, service; try { //Drill down to the service key and open it with write permission system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System"); currentControlSet = system.OpenSubKey("CurrentControlSet"); services = currentControlSet.OpenSubKey("Services"); service = services.OpenSubKey(conServiceName, true); //Delete any keys you created during installation (or that your service created) service.DeleteSubKeyTree("Parameters"); //... } catch (Exception e) { Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString()); } finally { //Let the project installer do its job base.Uninstall(savedState); } }
代碼添加完成後
添加window service安裝的批處理命令
1)在項目添加一個文本文件,改名爲install.bat,編輯文件的內容以下:
@echo off C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -i "WindowsService1.exe" @pause
2)在項目添加一個文本文件,改名爲uninstall.bat,編輯文件的內容以下
@echo off C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u "WindowsService1.exe" @pause
說明:上面綠色字體爲服務名稱
編譯完成後將debug的文件拷貝到想安裝的目錄下,點擊install.bat即完成安裝。