Linux 拨号vps windows公众号手机端

C#中net stop命令怎么使用

lewis 8年前 (2017-04-27) 阅读数 11 #程序编程
文章标签 c#

在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命令的输出结果。请确保在运行此代码时具有足够的权限来停止服务。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门