gpt4 book ai didi

c# - 从 Form App c# 启动停止服务

转载 作者:IT王子 更新时间:2023-10-29 04:03:03 24 4
gpt4 key购买 nike

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

最佳答案

添加对 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.");
}
}

关于c# - 从 Form App c# 启动停止服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com