2011-07-23 45 views
2

獲取二進制數據,我開始用代碼與下圖類似的過程:從StandardOutput

// some of the flags are not needed 
process.StartInfo.CreateNoWindow = true; 
process.StartInfo.ErrorDialog = false; 
process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardError = true; 
process.StartInfo.RedirectStandardOutput = true; 
process.EnableRaisingEvents = true; 
process.OutputDataReceived += process_OutputDataReceived; 
process.ErrorDataReceived += process_OutputDataReceived; 
process.Start(); 
process.BeginErrorReadLine(); 
process.BeginOutputReadLine(); 

void process_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
} 

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
} 

我遇到的問題是,​​對象有一個Data屬性,它是一個字符串。我需要將標準輸出數據作爲二進制數據讀取。我猜測沒有辦法將字符串數據恢復到適當的二進制數據中,所以使用不同方法接收二進制數據的任何建議都會很棒。

+1

請參閱「從Process.StandardOutput捕獲二進制輸出」(http://stackoverflow.com/questions/4143281/capturing-binary-output-from-process-standardoutput),儘管使用拉方法,而不是推(與正如你試圖做的那樣,OutputDataReceived事件處理程序)。 –

回答

2

對這個問題發表評論的Bradley Grainger是對的。事件處理程序不支持從標準輸出中檢索二進制數據。必須切換到使用主循環並使用讀取功能從標準數據中提取數據。

+1

剛剛發現了困難的方式。 –

相關問題