从 Form App c# 开始停止服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11062841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start stop Service from Form App c#
提问by AlexandruC
How can I start and stop a windows service from a c# Form application?
如何从 ac# Form 应用程序启动和停止 Windows 服务?
采纳答案by John Koerner
Add a reference to System.ServiceProcess.dll. Then you can use the ServiceControllerclass.
添加对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.");
}
}
回答by Adil
You can do it like this, Details of Service Controller
你可以这样做,服务控制器的详细信息
ServiceController sc = new ServiceController("your service name");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
Similarly you can stop using stop method
同样,您可以停止使用 stop 方法
sc.Stop();
回答by Christophe Geers
First add a reference to the System.ServiceProcess assembly.
首先添加对 System.ServiceProcess 程序集的引用。
To start:
开始:
ServiceController service = new ServiceController("YourServiceName");
service.Start();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
To stop:
停止:
ServiceController service = new ServiceController("YourServiceName");
service.Stop();
var timeout = new TimeSpan(0, 0, 5); // 5seconds
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
Both examples show how to wait until the service has reached a new status (running, stopped...etc.). The timeout parameter in WaitForStatus is optional.
这两个示例都显示了如何等待服务达到新状态(运行、停止等)。WaitForStatus 中的超时参数是可选的。
回答by Tomer W
there is a dirtier, but same same..
just execute the shell command
有一个更脏,但相同的..
只需执行shell命令
NET STOP "MYSERVICENAME"
NET START "MYSERVICENAME"
EDIT:
Would like to expand on "dirtier":
1. it is slower.
2. this can result in some clever vulnerabilities
3. it is a code that is not "understandable"
4. it is highly not portable (surely if you use .NET Core)
编辑:想扩展“更脏”:
1.它更慢。
2. 这可能会导致一些巧妙的漏洞
3. 它是一个无法“理解”的代码
4. 它非常不可移植(当然,如果你使用 .NET Core)
I am sure there are more wrong with it...
but if you search for a small, inhouse tool... it'll do.
(would like to say temporary, but temp stuff stick best!)
我相信它有更多的错误......
但是如果你寻找一个小的内部工具......它会做。
(想说临时的,但临时的东西最好坚持!)
回答by FAREH
// 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);
}
}

