如何从 ac# Form 应用程序启动和停止 Windows 服务?
问问题
56174 次
5 回答
75
添加对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.");
}
}
于 2012-06-16T11:07:02.863 回答
30
首先添加对 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 参数是可选的。
于 2012-06-16T11:09:03.153 回答
3
你可以这样做,服务控制器的详细信息
ServiceController sc = new ServiceController("your service name");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
同样,您可以停止使用 stop 方法
sc.Stop();
于 2012-06-16T11:05:23.283 回答
3
有一个更脏,但相同的..
只需执行shell命令
NET STOP "MYSERVICENAME"
NET START "MYSERVICENAME"
编辑:想扩展“更脏”:
1.速度较慢。
2. 这可能会导致一些巧妙的漏洞
3. 这是一个无法“理解”的代码
4. 它是高度不可移植的(如果你使用 .NET Core 肯定会这样)
我敢肯定它还有更多问题...
但是如果您搜索小型内部工具...就可以了。
(想说暂时的,但临时的东西最好!)
于 2012-06-16T11:09:19.233 回答
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);
}
}
于 2017-12-19T11:06:15.727 回答