从表单应用程序启动停止服务c#

时间:2012-06-16 11:01:53

标签: c# .net windows-services

如何从c#Form应用程序启动和停止Windows服务?

5 个答案:

答案 0 :(得分:63)

添加对System.ServiceProcess.dll的引用。然后你可以使用ServiceController类。

// Check whether the Alerter service is started.
ServiceController sc  = new ServiceController();
sc.ServiceName = "Alerter";
Console.WriteLine("The Alerter service status is currently set to {0}", 
                   sc.Status.ToString());

if (sc.Status == ServiceControllerStatus.Stopped)
{
   // Start the service if the current status is stopped.
   Console.WriteLine("Starting the Alerter service...");
   try 
   {
      // Start the service, and wait until its status is "Running".
      sc.Start();
      sc.WaitForStatus(ServiceControllerStatus.Running);

      // Display the current service status.
      Console.WriteLine("The Alerter service status is now set to {0}.", 
                         sc.Status.ToString());
   }
   catch (InvalidOperationException)
   {
      Console.WriteLine("Could not start the Alerter service.");
   }
}

答案 1 :(得分:20)

首先添加对System.ServiceProcess程序集的引用。

开始:

ServiceController service = new ServiceController("YourServiceName");
service.Start();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Running, timeout);

停止:

ServiceController service = new ServiceController("YourServiceName");
service.Stop();
 var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

这两个示例都显示了如何等待服务达到新状态(运行,停止等等)。 WaitForStatus中的timeout参数是可选的。

答案 2 :(得分:2)

你可以这样做,Details of Service Controller

ServiceController sc = new ServiceController("your service name");
if (sc.Status == ServiceControllerStatus.Stopped)
{
  sc.Start();

}

同样,您可以停止使用停止方法

  sc.Stop();

答案 3 :(得分:1)

有一个更脏,但同样相同..
只需执行shell命令

NET STOP "MYSERVICENAME"
NET START "MYSERVICENAME"

答案 4 :(得分:0)

// Check whether the U-TEST RTC service is started.
        ServiceController sc = new ServiceController();
        sc.ServiceName = "U-TEST RTC";
        m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStatusService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);

        if (sc.Status == ServiceControllerStatus.Stopped)
        {
            m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
            try
            {
                // Start the service, and wait until its status is "Running".
                sc.Start();
                var timeout = new TimeSpan(0, 0, 5); // 5seconds
                sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
                m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgNowService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
            }
            catch (InvalidOperationException)
            {
                m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgExceptionStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log);
            }
        }