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

问题描述 投票:33回答:5

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

c# .net windows-services
5个回答
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.");
   }
}

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
投票

你可以这样做,Details of Service Controller

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

}

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

  sc.Stop();

1
投票

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

NET STOP "MYSERVICENAME"
NET START "MYSERVICENAME"

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);
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.