从Process.StandardOutput重定向二进制数据会导致错误的数据 [英] Redirect binary data from Process.StandardOutput causes corrputed data

查看:69
本文介绍了从Process.StandardOutput重定向二进制数据会导致错误的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在中使用了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆