6

カスタム C# フォームで asp.net アプリケーションをプリコンパイルします。プロセス ログを取得して、プロセスが成功したかどうかを確認するにはどうすればよいですか?

これが私のコードです

string msPath = "c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\";
string msCompiler = "aspnet_compiler.exe";
string fullCompilerPath = Path.Combine(msPath, msCompiler);
msPath.ThrowIfDirectoryMissing();
fullCompilerPath.ThrowIfFileIsMissing();

ProcessStartInfo process = new ProcessStartInfo 
{ 
    CreateNoWindow = false,
    UseShellExecute = false,
    WorkingDirectory = msPath,
    FileName = msCompiler,
    Arguments = "-p {0} -v / {1}"
        .StrFormat(
            CurrentSetting.CodeSource,
            CurrentSetting.CompileTarget)
};

Process.Start(process);

ありがとう!

4

2 に答える 2

7

に設定ProcessStartInfo.RedirectStandardOutputしますtrue- これにより、すべての出力が にリダイレクトされますProcess.StandardOutput。これは、すべての出力メッセージを見つけるために読み取ることができるストリームです。

ProcessStartInfo process = new ProcessStartInfo 
{ 
   CreateNoWindow = false,
   UseShellExecute = false,
   WorkingDirectory = msPath,
   RedirectStandardOutput = true,
   FileName = msCompiler,
   Arguments = "-p {0} -v / {1}"
            .StrFormat(
              CurrentSetting.CodeSource, 
              CurrentSetting.CompileTarget)
};

Process p = Process.Start(process);
string output = p.StandardOutput.ReadToEnd();

OutputDataReceived@Bharath K が彼の回答で説明しているものと同様の方法でイベントを使用することもできます。

同様のプロパティ/イベントがあります - 同様に設定するStandardError必要があります。RedirectStandardErrortrue

于 2010-07-21T05:56:25.933 に答える
3

ソース アプリケーションで、ErrorDataReceived イベントを登録します。

StringBuilder errorBuilder = new StringBuilder( );
reportProcess.ErrorDataReceived += delegate( object sender, DataReceivedEventArgs e )
{
    errorBuilder.Append( e.Data );
};
//call this before process start
reportProcess.StartInfo.RedirectStandardError = true;
//call this after process start
reportProcess.BeginErrorReadLine( );

ターゲット アプリケーションでスローされたエラーは、これにデータを書き込むことができます。このようなもの:

Console.Error.WriteLine( errorMessage ) ;
于 2010-07-21T05:53:06.470 に答える