从Process.StandardOutput重定向二进制数据会导致错误的数据 [英] Redirect binary data from Process.StandardOutput causes corrputed data
问题描述
在中使用了Stream.CopyTo-Approach,以避免出现此情况问题。
我发现了问题。输出的重定向是正确的,输入的读取似乎是问题所在。所以我将代码从以下位置更改为:
使用(var streamReader = new StreamReader(configuration.Source))
{
process.StandardInput.Write(streamReader.ReadToEnd());
process.StandardInput.Flush();
process.StandardInput.Close();
}
至
使用(var fileStream = new StreamReader(configuration.Source))
{
fileStream.BaseStream.CopyTo(process.StandardInput.BaseStream);
process.StandardInput.Close();
}
不起作用!
对于所有可能遇到相同问题的人,此处为更正的代码:
var process = new Process
{
StartInfo =
{
参数= string.Format(@-display),
FileName = configuration.PathToExternalSift,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};
process.ErrorDataReceived + =(ProcessErrorDataReceived);
process.Start();
process.BeginErrorReadLine();
//读入文件
使用(var fileStream = new StreamReader(configuration.Source))
{
fileStream.BaseStream.CopyTo(process.StandardInput.BaseStream);
process.StandardInput.Close();
}
//将输出重定向到文件。
使用(var fileStream = new FileStream(configuration.Destination,FileMode.OpenOrCreate))
{
process.StandardOutput.BaseStream.CopyTo(fileStream);
}
流程。WaitForExit();
On top of this problem, I have another. I try to get binary data from a external process, but the data(a image) seems to be corrupted. The screenshot below shows the corruption: The left image was done by executing the program on command line, the right one from code.
My Code so far:
var process = new Process
{
StartInfo =
{
Arguments = string.Format(@"-display"),
FileName = configuration.PathToExternalSift,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};
process.ErrorDataReceived += (ProcessErrorDataReceived);
process.Start();
process.BeginErrorReadLine();
//Reads in pbm file.
using (var streamReader = new StreamReader(configuration.Source))
{
process.StandardInput.Write(streamReader.ReadToEnd());
process.StandardInput.Flush();
process.StandardInput.Close();
}
//redirect output to file.
using (var fileStream = new FileStream(configuration.Destination, FileMode.OpenOrCreate))
{
process.StandardOutput.BaseStream.CopyTo(fileStream);
}
process.WaitForExit();
Is this some kind of encoding problem? I used the Stream.CopyTo-Approach as mentioned here in order to avoid there problems.
I found the problem. The redirection of the output was correct, the reading of the input seems to be the problem. So I changed the code from:
using (var streamReader = new StreamReader(configuration.Source))
{
process.StandardInput.Write(streamReader.ReadToEnd());
process.StandardInput.Flush();
process.StandardInput.Close();
}
to
using (var fileStream = new StreamReader(configuration.Source))
{
fileStream.BaseStream.CopyTo(process.StandardInput.BaseStream);
process.StandardInput.Close();
}
Not it works!
For all the people that might have the same problem, here the corrected code:
var process = new Process
{
StartInfo =
{
Arguments = string.Format(@"-display"),
FileName = configuration.PathToExternalSift,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};
process.ErrorDataReceived += (ProcessErrorDataReceived);
process.Start();
process.BeginErrorReadLine();
//read in the file.
using (var fileStream = new StreamReader(configuration.Source))
{
fileStream.BaseStream.CopyTo(process.StandardInput.BaseStream);
process.StandardInput.Close();
}
//redirect output to file.
using (var fileStream = new FileStream(configuration.Destination, FileMode.OpenOrCreate))
{
process.StandardOutput.BaseStream.CopyTo(fileStream);
}
process.WaitForExit();
这篇关于从Process.StandardOutput重定向二进制数据会导致错误的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!