1. 程式人生 > >安裝Windows服務幾種方法

安裝Windows服務幾種方法

  安裝Winfows服務首先要新增安裝程式,新增安裝程式步驟如下:

1、將Windows服務程式切換到設計檢視, 右擊設計檢視選擇“新增安裝程式”

2、切換到剛被新增的ProjectInstaller的設計檢視


一般設定如下:

 設定serviceInstaller1元件的屬性:
    1) ServiceName = 服務名稱
    2) StartType = Automatic ,即自動
 設定serviceProcessInstaller1元件的屬性
    1) Account = LocalSystem,賬戶一般設定為本地系統

3、生成解決方案

安裝服務:

方法一、使用DOS命令安裝window服務

1、在服務所在的資料夾下的bin\debug資料夾下找到.exe檔案(例如WindowsService1.exe)

將此檔案拷貝到你想安裝的檔案夾中。

2、進入DOS介面

(VS2008-->Visual Studio Tools-->Visual Studio 2008 命令提示)來進入DOS,直接用cmd可能有些命令找不到;

3、輸入


方法二、使用安裝專案安裝windows服務

個人比較推薦這個方法,選擇目錄安裝更靈活,而且不用在DOS環境下執行。

因為本人比較懶,直接給出別人總結的地址

http://blog.csdn.net/dyzcode/article/details/6981547

注意,以後每次服務專案有更改的時候,需要編譯服務後,在安裝專案中重新整理依賴項!!!


方法三、

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", "描述語言
"); //(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即完成安裝。