关于System.Diagnostics.Process中重定向的stdout

时间:2010-05-29 05:23:52

标签: c# .net stream

我最近一直致力于使用flac.exe和lame.exe在C#中将flac文件转换为mp3的程序,以下是完成这项工作的代码:

ProcessStartInfo piFlac = new ProcessStartInfo( "flac.exe" );
piFlac.CreateNoWindow = true;
piFlac.UseShellExecute = false;
piFlac.RedirectStandardOutput = true;
piFlac.Arguments = string.Format( flacParam, SourceFile );

ProcessStartInfo piLame = new ProcessStartInfo( "lame.exe" );
piLame.CreateNoWindow = true;
piLame.UseShellExecute = false;
piLame.RedirectStandardInput = true;
piLame.RedirectStandardOutput = true;
piLame.Arguments = string.Format( lameParam, QualitySetting, ExtractTag( SourceFile ) );

Process flacp = null, lamep = null;
byte[] buffer = BufferPool.RequestBuffer();


flacp = Process.Start( piFlac );
lamep = new Process();
lamep.StartInfo = piLame;
lamep.OutputDataReceived += new DataReceivedEventHandler( this.ReadStdout );
lamep.Start();
lamep.BeginOutputReadLine();

int count = flacp.StandardOutput.BaseStream.Read( buffer, 0, buffer.Length );

while ( count != 0 )
{
    lamep.StandardInput.BaseStream.Write( buffer, 0, count );

    count = flacp.StandardOutput.BaseStream.Read( buffer, 0, buffer.Length );
}

这里我设置命令行参数告诉lame.exe将其输出写入stdout,并利用Process.OutPutDataRecerved事件来收集输出数据,这主要是二进制数据,但是DataReceivedEventArgs.Data是键入“string”,我必须将它转换为byte [],然后再将其转换为缓存,我认为这很难看,我尝试了这种方法,但结果不正确。

有没有办法可以绕过OutputDataReceived事件同步或异步读取原始重定向的stdout流?

PS:我不使用lame直接写入磁盘的原因是我试图并行转换多个文件,直接写入磁盘会导致严重的碎片。

非常感谢!

1 个答案:

答案 0 :(得分:0)

我不认为lame和flac使用Stdout输出转换后的数据。我认为他们将转换后的文件直接写入磁盘。 StdOut仅用于输出信息/错误消息。