【问题标题】:Capture standard output of another command-line program, with characters not in current codepage捕获另一个命令行程序的标准输出,字符不在当前代码页中
【发布时间】:2015-01-03 15:30:28
【问题描述】:

我正在使用 .NET ProcessStartInfo.RedirectStandardOutputProcess.BeginOutputReadLine 来捕获另一个程序(特别是 youtube-dl.exe)的标准输出并将它们保存到字符串变量中。

但是结果变量只包含我的语言的常用字符,例如繁体中文和英文字母;带有重音符号的韩语或拉丁字母等字符完全消失了。

File.WriteAllText-ed 结果变量并使用多个文本编辑器检查了文件,所以我确定它们丢失了,而不是它们存在并且只是无法通过控制台窗口显示。

在 Windows 命令提示符中直接执行 youtube-dl 会显示包含这些外来字符的完整消息。

我的youtubeDL_process.OutputDataReceived 很简单:

(s, e) => {
  if(!string.IsNullOrWhiteSpace(e.Data)) this._filename = e.Data;
}

如何使重定向后的带外来字符的标准输出字符串完整,就像在命令提示符中直接生成的一样?

【问题讨论】:

    标签: .net redirect process character-encoding stdout


    【解决方案1】:

    您直接将输出直接保存到文件中。试试这个

    youtubeDL_process.OutputDataReceived += (s, e) => CaptureOutput(e.Data);
    youtubeDL_process.Start();
    youtubeDL_process.BeginOutputReadLine();
    
    
    private void CaptureOutput(string s)
    {
        Console.WriteLine("\r\n" + s);
        //write fileChunks here using a StreamWriter.... 
    }
    

    //因为 CaptureOutput 正在以字符串形式接收您的数据,请尝试使用其他类型(例如二进制)以免去除字符串中的特殊字符(外来字符),还要检查一些 UTF8 编码或任何其他编码方法文档.

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      相关资源
      最近更新 更多