如何在C#中运行隐藏的CMD?

3
如何在不弹出CMD窗口的情况下从C#运行CMD?

请查看https://dev59.com/-3NA5IYBdhLWcg3wF5qO,这与您想要做的非常相似。 - Jagmag
1个回答

9
ProcessStartInfo中有一个名为CreateNoWindow的参数。
public static string ExecuteCommand(string command) {

    ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command)
        {
            RedirectStandardError = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

    using (Process proc = new Process())
    {
        proc.StartInfo = procStartInfo;
        proc.Start();

        string output = proc.StandardOutput.ReadToEnd();

        if (string.IsNullOrEmpty(output))
            output = proc.StandardError.ReadToEnd();

        return output;
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接