关于重定向的标准输出中的System.Diagnostics.Process重定向、标准、System、Diagnostics

2023-09-06 23:58:29 作者:-歪果仁

我最近工作的一个程序,使用flac.exe和lame.exe转换FLAC文件到MP3在C#中,这里有code,它做的工作:

I've been recently working on a program that convert flac files to mp3 in C# using flac.exe and lame.exe, here are the code that do the job:

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写其输出到标准输出,并利用Process.OutPutDataRecerved事件收集的输出数据,其中大部分是二进制数据,但DataReceivedEventArgs.Data是键入串,我必须把它转换为byte []之前,把它放到缓存中,我认为这是丑陋的,我试过这种方法,但结果不正确。

Here I set the command line parameters to tell lame.exe to write its output to stdout, and make use of the Process.OutPutDataRecerved event to gather the output data, which is mostly binary data, but the DataReceivedEventArgs.Data is of type "string" and I have to convert it to byte[] before put it to cache, I think this is ugly and I tried this approach but the result is incorrect.

有没有什么办法,我可以读出原始重定向标准输出流,同步或异步,绕过OutputDataReceived事件?

Is there any way that I can read the raw redirected stdout stream, either synchronously or asynchronously, bypassing the OutputDataReceived event?

PS:为什么我不使用瘸子写入磁盘,直接的原因是,我想转换并行多个文件,并直接写入到磁盘会导致严重的碎片

PS: the reason why I don't use lame to write to disk directly is that I'm trying to convert several files in parallel, and direct writing to disk will cause severe fragmentation.

非常感谢!

推荐答案

我不认为瘸子,后手采用标准输出输出其转换后的数据。我觉得他们写的转换后的文件直接到磁盘。标准输出仅用于输出方式/错误消息。

I don't think lame and flac uses Stdout to output its converted data. I think they write the converted files directly to disk. The StdOut is only used to output info/error messages.