2009-06-24 343 views
103

我有使用InstallUtil.exe安裝的Windows服務。即使我已將「啓動方法」設置爲「自動」,但安裝後服務不會啓動,但我必須手動打開服務並單擊「開始」。有沒有辦法通過命令行或通過服務的代碼啓動它?安裝時自動啓動Windows服務

回答

180

在你安裝程序類中,添加的處理程序AfterInstall事件。然後您可以調用事件處理程序中的ServiceController來啓動該服務。

using System.ServiceProcess; 

public ServiceInstaller() 
{ 
    //... Installer code here 
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall); 
} 

void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) 
{ 
    using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName)) 
    { 
     sc.Start(); 
    } 
} 

現在,當您在安裝程序上運行InstallUtil時,它將安裝並啓動該服務。

0

自動啓動意味着服務在Windows啓動時自動啓動。正如其他人所說,要從控制檯啓動它,您應該使用ServiceController。

+0

我不想這樣做。我期望從命令行或Windows服務類中執行此操作。 – mickyjtwin 2009-06-24 06:41:41

+0

對不起,我的錯,我錯過了你明確排除通過控制面板啓動它的可能性。 – 2009-06-24 06:54:34

5

下面的命令怎麼樣?

net start "<service name>" 
net stop "<service name>" 
3

您可以使用下面的命令行來啓動服務:

net start *servicename* 
2

使用ServiceController從代碼開始爲您服務。

更新:從命令行啓動服務的更正確方法是使用「sc」(Service Controller)命令而不是「net」。

+6

爲什麼「sc」是「更正確」的方式? 「net start」(和啓動服務PSH cmdlet)有什麼問題? – Richard 2009-06-24 08:14:45

+1

因爲可以從遠程機器調用sc,所以它始終有效。 – MacGyver 2015-12-04 01:30:31

0

可以使用GetServices方法ServiceController 類來獲取所有服務的數組。然後,通過檢查每個服務的ServiceName屬性來找到您的服務。當您找到您的服務時,請致電Start方法啓動它。

您還應該檢查Status屬性以查看在調用開始之前它已處於什麼狀態(它可能正在運行,已暫停,已停止等)。

4

控制服務編程選項:

  • 本機代碼可以使用,"Starting a Service"。最大的控制與最小的依賴性,但最多的工作。
  • WMI:Win32_Service有一個StartService方法。這對於需要執行其他處理(例如選擇哪種服務)的情況非常有用。
  • PowerShell:通過RunspaceInvoke執行Start-Service或創建自己的Runspace並使用其CreatePipeline方法執行。這對於需要能夠使用比WMI更容易的編碼模型執行其他處理(例如選擇哪種服務)的情況是很好的,但取決於安裝的PSH。
  • .NET應用程序可以使用ServiceController
23

重構一點點之後,這是帶自動啓動一個完整的Windows安裝程序服務的一個例子:

using System.ComponentModel; 
using System.Configuration.Install; 
using System.ServiceProcess; 

namespace Example.of.name.space 
{ 
[RunInstaller(true)] 
public partial class ServiceInstaller : Installer 
{ 
    private readonly ServiceProcessInstaller processInstaller; 
    private readonly System.ServiceProcess.ServiceInstaller serviceInstaller; 

    public ServiceInstaller() 
    { 
     InitializeComponent(); 
     processInstaller = new ServiceProcessInstaller(); 
     serviceInstaller = new System.ServiceProcess.ServiceInstaller(); 

     // Service will run under system account 
     processInstaller.Account = ServiceAccount.LocalSystem; 

     // Service will have Start Type of Manual 
     serviceInstaller.StartType = ServiceStartMode.Automatic; 

     serviceInstaller.ServiceName = "Windows Automatic Start Service"; 

     Installers.Add(serviceInstaller); 
     Installers.Add(processInstaller); 
     serviceInstaller.AfterInstall += ServiceInstaller_AfterInstall;    
    } 
    private void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) 
    { 
     ServiceController sc = new ServiceController("Windows Automatic Start Service"); 
     sc.Start(); 
    } 
} 
} 
0

你破壞你的設計師。重新添加您的安裝程序組件。它應該有一個serviceInstaller和一個serviceProcessInstaller。將屬性啓動方法設置爲自動的serviceInstaller將在安裝時以及每次重新啓動後啓動。

1

儘管完全按照接受的答案,我仍然無法啓動服務 - 我在安裝過程中收到了失敗消息,指出剛安裝的服務無法啓動,因爲它不存在儘管使用this.serviceInstaller.ServiceName而不是字面...

我終於找到一個替代的解決方案,使得使用命令行:

private void serviceInstaller_AfterInstall(object sender, InstallEventArgs e) { 
     ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startInfo.FileName = "cmd.exe"; 
     startInfo.Arguments = "/C sc start " + this.serviceInstaller.ServiceName; 

     Process process = new Process(); 
     process.StartInfo = startInfo; 
     process.Start(); 
    } 
0

剛一說明:您可能以不同方式利用建立的爲您服務窗體界面添加服務安裝程序和項目安裝程序。在這種情況下,請將serviceInstaller.ServiceName替換爲「name from designer」.ServiceName。

在這種情況下,您也不需要私有成員。

感謝您的幫助。

相關問題