温馨提示×

C#中net stop命令怎么使用

c#
小亿
131
2024-01-10 10:40:33
栏目: 编程语言

在C#中,可以使用Process.Start()方法来执行net stop命令。以下是一个示例代码:

using System; using System.Diagnostics; class Program { static void Main() { string serviceName = "MyService"; // 指定要停止的服务名 Process process = new Process(); process.StartInfo.FileName = "net"; process.StartInfo.Arguments = $"stop {serviceName}"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.CreateNoWindow = true; process.Start(); // 读取命令的输出结果 string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); Console.WriteLine(output); } } 

这个代码将会停止名为MyService的服务,并输出net stop命令的输出结果。请确保在运行此代码时具有足够的权限来停止服务。

0