How to use the net stop command in C#?
In C#, you can use the Process.Start() method to execute the net stop command. Here is an example code:
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);
}
}
This code will stop the service named MyService and output the result of the net stop command. Make sure you have sufficient permissions to stop the service when running this code.