如何在 C# 中使用 net stop 命令启动/停止服务

发布于 2024-07-26 05:36:43 字数 450 浏览 5 评论 0原文

如何在 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()

我使用了进程类但没有结果

how do start/stop services using net stop command in c#
for example

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终遇你 2024-08-02 05:36:43

您可以使用 ServiceController 类来启动/停止本地/远程计算机上的特定服务。

using System.ServiceProcess;
ServiceController controller  = new ServiceController();

controller.MachineName = ".";
controller.ServiceName = "mysql";

// Start the service
controller.Start();

// Stop the service
controller.Stop();

Instead of using a crude method like Process.Start, you can use the ServiceController class to start/stop a particular service on a local/remote machine.

using System.ServiceProcess;
ServiceController controller  = new ServiceController();

controller.MachineName = ".";
controller.ServiceName = "mysql";

// Start the service
controller.Start();

// Stop the service
controller.Stop();
桜花祭 2024-08-02 05:36:43

您可能想查看 System.ServiceProcess.ServiceController< /a> 类,它提供 Windows 服务的托管接口。

在这种情况下:

var mysql = new System.ServiceProcess.ServiceController("mysql");
if (mysql .Status == ServiceControllerStatus.Stopped) {
   mysql.Start();
}

You might want to take a look at the System.ServiceProcess.ServiceController class, which provides a managed interface to Windows' Services.

In this case:

var mysql = new System.ServiceProcess.ServiceController("mysql");
if (mysql .Status == ServiceControllerStatus.Stopped) {
   mysql.Start();
}
决绝 2024-08-02 05:36:43

您需要将“/c”开关传递给cmd.exe

pstart.Arguments = "/c net start mysql"

You need to pass the "/c" switch to cmd.exe

pstart.Arguments = "/c net start mysql"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文