Linux 拨号vps windows公众号手机端

c#怎么调用cmd并输入参数

lewis 1年前 (2024-04-26) 阅读数 20 #程序编程
文章标签 c#

在C#中调用cmd并输入参数可以通过Process类实现。下面是一个示例代码:

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
        string cmdPath = "cmd.exe";
        string arguments = "/C echo Hello World";

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = cmdPath;
        startInfo.Arguments = arguments;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        string output = process.StandardOutput.ReadToEnd();
        Console.WriteLine(output);

        process.WaitForExit();
    }
}

在上面的示例代码中,我们调用了cmd并使用参数/C echo Hello World来输出"Hello World"。您可以根据需要修改cmdPatharguments变量来执行不同的命令和参数。

版权声明

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

发表评论:

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

热门