2017-04-01 141 views
0

我創建了一個用於安裝服務的批處理文件,因爲我需要在PC上安裝我的服務沒有Visual Studio。如何通過SC安裝後自動啓動Windows服務?

內容批處理文件:

@echo OFF 
echo Installing service... 
sc create "MyService" binpath= %~dp0\MyService.exe start= auto 
echo Installing service complete 
pause 

,我需要自動啓動的MyService安裝後,所以我讓這樣的代碼:

private void svInstaller_AfterInstall(object sender, InstallEventArgs e) 
{ 
    ServiceController sc = new ServiceController(svInstaller.ServiceName); 
    sc.Start(); 
} 

不要任何問題,如果我通過視覺安裝我的服務使用InstallUtil的Studio命令提示符。 當我通過批處理文件安裝服務時,我的服務沒有自動啓動。

如何通過批處理文件安裝後自動啓動我的服務?

更新:感謝Sam Denty的回答,我的問題得到解決。
但我有另一個問題:當我通過sc安裝我的服務時,我的AfterInstall函數中的代碼不起作用?

回答

0

這可以通過使用net start servicesc start命令(see previous question on that)。

要使用sc start啓動服務,語法是:

sc [<ServerName>] start <ServiceName> [<ServiceArguments>] 

<ServerName> 
    Specifies the name of the remote server on which the service is located. The name must use the Universal Naming Convention (UNC) format (for example, \\myserver). To run SC.exe locally, omit this parameter. 
<ServiceName> 
    Specifies the service name returned by the getkeyname operation. 
<ServiceArguments> 
    Specifies the service arguments to pass to the service to be started. 

實施例:

sc start MyService 


更新腳本:

@echo OFF 
echo Installing service... 
sc create "MyService" binpath= %~dp0\MyService.exe start= auto 
sc start MyService 
echo Installing service complete 
pause 
+0

這很簡單。也許,我使用了錯誤的關鍵字搜索。但這意味着在安裝sc後,afterInstall函數中的代碼不起作用? –

相關問題