c#run命令不执行命令

Das*_*abs 0 c# command-line process

我想运行这个:

string command = "echo test > test.txt";
System.Diagnostics.Process.Start("cmd.exe", command);
Run Code Online (Sandbox Code Playgroud)

它不起作用,我做错了什么?

Dar*_*rov 13

您缺少将/C开关传递cmd.exe给指示您要执行命令.另请注意,该命令放在双引号中:

string command = "/C \"echo test > test.txt\"";
System.Diagnostics.Process.Start("cmd.exe", command).WaitForExit();
Run Code Online (Sandbox Code Playgroud)

如果您不想看到shell窗口,可以使用以下内容:

string command = "/C \"echo test > test.txt\"";
var psi = new ProcessStartInfo("cmd.exe")
{
    Arguments = command,
    UseShellExecute = false,
    CreateNoWindow = true
};

using (var process = Process.Start(psi))
{
    process.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)