在 C# 中通过 Visual Studio 将命令传递给 cmd

本文关键字:命令 cmd Studio Visual | 更新日期: 2023-09-27 18:33:53

我正在尝试将命令传递给cmd,并在文本框中读取Visual Studio中cmd给出的输出。我将如何做?

在 C# 中通过 Visual Studio 将命令传递给 cmd

我假设你的意思是在你的应用程序中你是在可视化工作室中

// Use Process to start (and execute) the `cmd`:
var cmd = new Process
{
    StartInfo = newProcessStartInfo
    {
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        UseShellExecute = false,
        Filename = "cmd.com",
        //Arguments = "foo bar baz" // if necessary
    }
};
// run it
cmd.Start();
// then get the returned output and place it in your textbox
while (!cmd.StandardOutput.EndOfStream)
{
    String content = cmd.StandardOutput.ReadLine();
    // textbox1.Text += content;
}

否则,如果您只想在开发时复制 CMD 输出,只需运行该命令,然后使用应用程序菜单选择并复制文本。