0

これを実行したい:

string command = "echo test > test.txt";
System.Diagnostics.Process.Start("cmd.exe", command);

うまくいきません。何が間違っていますか?

4

2 に答える 2

13

コマンドを実行することを示すために/Cスイッチを渡す必要がありません。cmd.exeコマンドが二重引用符で囲まれていることにも注意してください。

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

また、シェル ウィンドウを表示したくない場合は、次を使用できます。

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();
}
于 2012-12-24T11:24:28.643 に答える
0

これで始められるはずです:

//create your command
string cmd = string.Format(@"/c echo Hello World > mydata.txt");
//prepare how you want to execute cmd.exe
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.Arguments = cmd;//<<pass in your command
//this will make echo's and any outputs accessiblen on the output stream
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process p = Process.Start(psi);
//read the output our command generated
string result = p.StandardOutput.ReadToEnd();
于 2012-12-24T11:25:35.680 に答える