3

Process クラスを使用して、次のように WPF C# アプリケーションで (Putty から) pscp.exe を実行します。

Process p = new Process();
p.StartInfo.FileName = "pscp.exe";
p.StartInfo.Arguments = "-v -scp -pw xx toto@192.168.0.1:/path .";
p.Start();
p.WaitForExit();

プロセスはコマンド プロンプトを開き、正しく終了します。問題ありません。

ここで、プロセスの stderr ファイルにログインしたいと思いますが、問題ありません。

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;

ファイルにストリームを出力しますが、シェルには何も表示されません (これはリダイレクトです)。

私のニーズは、シェルにstderrを表示し、それをログファイルにコピーすることです(リダイレクトなし、2回必要でした)。

編集:私のニーズを表す明確な例:この関数を呼び出す1つのボタンを持つWPFアプリケーションを想像してください:

Process p = new Process();
p.StartInfo.FileName = "pscp.exe";
p.StartInfo.Arguments = "-v -scp -pw xx toto@192.168.0.1:/path .";
p.Start();
p.WaitForExit();

プロセスは cmd プロンプト (stdout + stderr を出力) で始まり、同時に stderr がログ ファイルに出力されます。

これを行うアイデアはありますか?

4

2 に答える 2

0

Hylaean で説明されているように: まず、Process オブジェクトを準備し、イベントを登録します。

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.ErrorDataReceived += new DataReceivedEventHandler(shell_ErrorDataReceived);
p.OutputDataReceived += new DataReceivedEventHandler(shell_OutputDataReceived);

shellOut = openWriter(outputFileName);   //  opens a StreamWriter

イベント ハンドラーでは、stderr および stdout データで好きなことを行うことができます。

// write out info to the display window
private static void shell_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    string s = e.Data;

    if ((s != null) && (shellOut != null))
    {
        SomeOutOrLog(s);         //  output or log the string to somewhere
        shellOut.WriteLine(s);
    }
}

private static void shell_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    string s = e.Data;

    if (s != null) 
    {
        SomeOutOrLog(s);   //  output or log the string to somewhere
    }
}
于 2013-01-03T17:13:20.300 に答える
0

ErrorDataReceivedEvent を購読する必要があります

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.errordatareceived.aspx

Process p = new Process();
p.StartInfo.FileName = "pscp.exe";
p.StartInfo.Arguments = "-v -scp -pw xx toto@192.168.0.1:/path .";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += (o, e) => Console.Error.WriteLine(e.Data);
p.Start();
p.WaitForExit();
于 2012-12-07T11:45:34.670 に答える