2011-04-29 128 views

回答

3

您想使用ServiceController class

用你的服務名稱初始化它,並調用Start()和Stop()方法。

using System.ServiceProcess; 
ServiceController sc = new ServiceController("My service name"); 
if (sc.Status == ServiceControllerStatus.Stopped) 
{ 
    sc.Start(); 
} 
//etc.. 
+0

我試過但我得到一個拒絕訪問異常。 – xaria 2011-05-04 02:55:21

+0

@xaria:您的服務可能需要提升權限才能被控制,在這種情況下,以管理員身份運行您的程序 – CharlesB 2011-05-04 06:49:46

+0

是的,情況就是如此。謝謝 – xaria 2011-05-06 07:32:13

相關問題