我有使用InstallUtil.exe安裝的Windows服務。即使我已將「啓動方法」設置爲「自動」,但安裝後服務不會啓動,但我必須手動打開服務並單擊「開始」。有沒有辦法通過命令行或通過服務的代碼啓動它?安裝時自動啓動Windows服務
回答
在你安裝程序類中,添加的處理程序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時,它將安裝並啓動該服務。
自動啓動意味着服務在Windows啓動時自動啓動。正如其他人所說,要從控制檯啓動它,您應該使用ServiceController。
下面的命令怎麼樣?
net start "<service name>"
net stop "<service name>"
您可以使用下面的命令行來啓動服務:
net start *servicename*
使用ServiceController從代碼開始爲您服務。
更新:從命令行啓動服務的更正確方法是使用「sc」(Service Controller)命令而不是「net」。
可以使用GetServices
方法ServiceController 類來獲取所有服務的數組。然後,通過檢查每個服務的ServiceName
屬性來找到您的服務。當您找到您的服務時,請致電Start
方法啓動它。
您還應該檢查Status
屬性以查看在調用開始之前它已處於什麼狀態(它可能正在運行,已暫停,已停止等)。
控制服務編程選項:
- 本機代碼可以使用,"Starting a Service"。最大的控制與最小的依賴性,但最多的工作。
- WMI:Win32_Service有一個
StartService
方法。這對於需要執行其他處理(例如選擇哪種服務)的情況非常有用。 - PowerShell:通過
RunspaceInvoke
執行Start-Service
或創建自己的Runspace
並使用其CreatePipeline
方法執行。這對於需要能夠使用比WMI更容易的編碼模型執行其他處理(例如選擇哪種服務)的情況是很好的,但取決於安裝的PSH。 - .NET應用程序可以使用
ServiceController
重構一點點之後,這是帶自動啓動一個完整的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();
}
}
}
你破壞你的設計師。重新添加您的安裝程序組件。它應該有一個serviceInstaller和一個serviceProcessInstaller。將屬性啓動方法設置爲自動的serviceInstaller將在安裝時以及每次重新啓動後啓動。
儘管完全按照接受的答案,我仍然無法啓動服務 - 我在安裝過程中收到了失敗消息,指出剛安裝的服務無法啓動,因爲它不存在儘管使用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();
}
剛一說明:您可能以不同方式利用建立的爲您服務窗體界面添加服務安裝程序和項目安裝程序。在這種情況下,請將serviceInstaller.ServiceName替換爲「name from designer」.ServiceName。
在這種情況下,您也不需要私有成員。
感謝您的幫助。
- 1. 安裝時自動啓動Windows服務
- 2. 自動啓動服務時,windows啓動
- 3. 安裝後自動啓動服務
- 4. 安裝Windows服務將無法啓動
- 5. 安裝Windows服務啓動參數
- 6. 安裝後Windows服務沒有自動啓動
- 7. 如何通過SC安裝後自動啓動Windows服務?
- 8. MSI安裝程序啓動服務啓動時的自動修復
- 9. C#安裝服務並設置爲在啓動時自動啓動
- 10. 如何將程序安裝爲在啓動時自動啓動的服務?
- 11. Windows服務啓動時啓動
- 12. 如何使Windows服務啓動「自動(延時啓動)」
- 13. 當系統重新啓動時自動啓動windows服務
- 14. 在Windows 8中啓動時自動啓動node.js服務器
- 15. 無法啓動作爲Windows服務安裝的WCF服務
- 16. 服務自動化安裝
- 17. 作爲Windows服務安裝時啓動遠程調試Tomcat(jdwp)
- 18. Windows服務啓動時間
- 19. Windows服務啓動超時
- 20. 啓動時啓動服務和應用程序安裝後
- 21. Windows服務不會自動啓動
- 22. 我如何啓動Windows服務自動
- 23. Windows服務不能自動啓動
- 24. Python Windows服務自動啓動太早
- 25. 在沒有安裝項目的情況下安裝時啓動Windows服務
- 26. 只要Windows啓動時自動運行Dropbox作爲Windows服務
- 27. 安裝sqlxml windows azure啓動任務
- 28. 啓動時自動安裝.vhdx
- 29. 如何檢查是否安裝了服務並啓動服務(如果安裝了其他服務,如果安裝並啓動Windows的廚師的服務)
- 30. 在Windows啓動時自動啓動Windows服務,這取決於Oracle
我不想這樣做。我期望從命令行或Windows服務類中執行此操作。 – mickyjtwin 2009-06-24 06:41:41
對不起,我的錯,我錯過了你明確排除通過控制面板啓動它的可能性。 – 2009-06-24 06:54:34