2012-09-12 2 views
1
try 
{ 
     string filename = "E:\\sox-14-4-0\\mysamplevoice.wav"; 
     Process p = new Process(); 
     p.StartInfo.UseShellExecute = false; 
     p.StartInfo.RedirectStandardOutput = true; 
     p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe "; 
     p.StartInfo.Arguments = filename + " -n stat"; 
     p.Start(); 
     string output = p.StandardOutput.ReadToEnd(); 
     p.WaitForExit(); 
} 
catch(Exception Ex) 
{ 
     Console.WriteLine(Ex.Message); 
} 

출력은 항상 비어 있습니다. 내가 명령 프롬프트에서 sox 명령은 내가 같은 응답을 얻을 수 있음을 실행하는 경우 : C#에서 동일한 명령을 실행하는 경우Sox는 값을 반환하지만 StandardOutput.ReadToEnd()는 빈 값을 반환합니다.

E:\sox-14-4-0>sox mysamplevoice.wav -n stat 
Samples read:    26640 
Length (seconds):  3.330000 
Scaled by:   2147483647.0 
Maximum amplitude:  0.515625 
Minimum amplitude: -0.734375 
Midline amplitude: -0.109375 
Mean norm:   0.058691 
Mean amplitude:  0.000122 
RMS  amplitude:  0.101146 
Maximum delta:   0.550781 
Minimum delta:   0.000000 
Mean delta:   0.021387 
RMS  delta:   0.041831 
Rough frequency:   526 
Volume adjustment:  1.362 

을 내가 같은 결과 그러나 "출력"의 가치를 얻을 비어 있습니다.

+0

를 추가 할 수도이 문제에 실행, 새로운 메신저 N 난 some1 잘못을 편집했다 생각했다. 사과를하고 다시하지는 ​​않을 것입니다. –

+0

이름 끝에 ♦이있는 사용자는 운영자이므로 일반적으로 해당 작업을 되돌려서는 안됩니다. 질문이 있거나 편집 한 이유를 이해할 수 없다면 @TheEditorsNickname이 포함 된 주석을 사용하여 편집기에 "핑"할 수 있습니다. – Mat

+0

@Mat : 귀하의 정보에 감사드립니다. –

답변

4

sox.exe가 STDOUT에 쓰고 STDERR에 쓰지는 않습니까?

대신 OutputDataReceived 이벤트를 사용하여 데이터를 읽을 수 있습니다.

string filename = "E:\\sox-14-4-0\\mysamplevoice.wav"; 
Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardError = true; 
p.StartInfo.FileName = "E:\\sox-14-4-0\\sox.exe "; 
p.StartInfo.Arguments = filename + " -n stat"; 

p.OutputDataReceived += process_OutputDataReceived; 
p.ErrorDataReceived += process_ErrorDataReceived; 

p.Start(); 
p.BeginErrorReadLine(); 
p.BeginOutputReadLine(); 


void process_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    string s = e.Data; 
} 

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    string s = e.Data; 
} 
+0

감사합니다. 코드를 사용해보십시오. "표준 오류가 리디렉션되지 않았습니다."라는 예외 오류가 발생합니다. 이것은 디버그가 p.BeginErrorReadLine()에 도달 할 때 발생합니다. –

+2

'p.StartInfo.RedirectStandardError = true; '를 추가하십시오. –

+0

예. 그 라인을 놓쳤다. 추가되었습니다. 감사합니다 @ ChibuezeOpata –

0

방금이 문제도 발생했습니다. SoX가 StandardError에 쓰는 이유는 무엇입니까? 다른

경우 누군가가 원래의 질문에 대한 해결책은 단 2 개 라인

내가 모르는
p.StartInfo.RedirectStandardError = true; // <-- this 
... 
string output = p.StandardOutput.ReadToEnd(); 
if(output == "") output = p.StandardError.ReadToEnd(); // <-- and this 
관련 문제