Process::Start(userProcess)

abcde
記事: 0
登録日時: 14年前

Process::Start(userProcess)

投稿記事 by abcde » 14年前

Process::Start にてコマンドを実行する際に、
パラメータを2つ以上渡すにはどうすればいいんだろうか・・・。

使ったのは VisualC++ 2008 Express Edition

CODE:

System::Diagnostics::ProcessStartInfo^ userProcess
  = gcnew System::Diagnostics::ProcessStartInfo;
userProcess->FileName = L"ping";
userProcess->Arguments =
  L"google.co.jp > dummy.txt";
System::Diagnostics::Process::Start(userProcess);
パラメータが1つだけなら問題なくできた。
 ping google.co.jp

でも
 ping google.co.jp > dummy.txt
はうまく生成できないようで。

なぜだ・・・・・・。


被災地のために募金しました。
物資輸送も計画していたのですが、個人から送っても受け付けてもらえないらしい。
モノが不足していると聞いたものの・・・。
最後に編集したユーザー abcde on 2011年3月16日(水) 22:37 [ 編集 2 回目 ]

ISLe
記事: 2650
登録日時: 15年前

RE: Process::Start(userProcess)

投稿記事 by ISLe » 14年前

リダイレクトはコマンドプロセッサの機能なのでpingコマンドが">"パラメータを処理できないのです。

コマンドプロセッサにコマンド全部投げる。

CODE:

userProcess->FileName = L"cmd.exe";
userProcess->Arguments = L"/C ping google.co.jp > dummy.txt";
System::Diagnostics::Process::Start(userProcess);
リダイレクト結果をプログラムで読み取ってファイルに書き出す。

CODE:

userProcess->FileName = L"ping";
userProcess->Arguments = L"google.co.jp";
userProcess->RedirectStandardOutput = true;
userProcess->UseShellExecute = false;
System::Diagnostics::Process^ p = System::Diagnostics::Process::Start(userProcess);
p->WaitForExit();
System::IO::StreamWriter^ out = gcnew System::IO::StreamWriter(L"dummy.txt", false);
while (p->StandardOutput->Peek() >= 0) {
	out->WriteLine(p->StandardOutput->ReadLine());
}
out->Close();

abcde
記事: 0
登録日時: 14年前

RE: Process::Start(userProcess)

投稿記事 by abcde » 14年前

おおおお、そうでした(・O・;)
リダイレクトは ping等 のパラメータではないですね。

コードまでご丁寧にありがとうございます!!