如何在c#中使用net stop命令启动/停止服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1113000/
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
how do start/stop services using net stop command in c#
提问by Suriyan Suresh
how do start/stop services using net stop command in c# for example
例如,如何在 c# 中使用 net stop 命令启动/停止服务
Dim pstart As New ProcessStartInfo
Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.System)
Dim p As New Process
pstart.FileName = path + "\cmd.exe"
pstart.UseShellExecute = False
pstart.CreateNoWindow = True
pstart.WorkingDirectory = path
pstart.FileName = "cmd.exe"
pstart.Arguments = " net start mysql"
p.StartInfo = pstart
p.Start()
i have used process class but no result
我使用过流程类但没有结果
采纳答案by Kirtan
Instead of using a crude method like Process.Start, you can use the ServiceControllerclass to start/stop a particular service on a local/remote machine.
您可以使用ServiceController类来启动/停止本地/远程机器上的特定服务,而不是使用像 Process.Start 这样的粗略方法。
using System.ServiceProcess;
ServiceController controller = new ServiceController();
controller.MachineName = ".";
controller.ServiceName = "mysql";
// Start the service
controller.Start();
// Stop the service
controller.Stop();
回答by heavyd
You need to pass the "/c" switch to cmd.exe
您需要将“/c”开关传递给 cmd.exe
pstart.Arguments = "/c net start mysql"
回答by Alex Lyman
You might want to take a look at the System.ServiceProcess.ServiceControllerclass, which provides a managed interface to Windows' Services.
您可能需要查看System.ServiceProcess.ServiceController类,该类提供了 Windows 服务的托管接口。
In this case:
在这种情况下:
var mysql = new System.ServiceProcess.ServiceController("mysql");
if (mysql .Status == ServiceControllerStatus.Stopped) {
mysql.Start();
}