【问题标题】:System.Diagnostics.Process.StandardOutput returning bad string that have accentuationSystem.Diagnostics.Process.StandardOutput 返回有重音的坏字符串
【发布时间】:2013-07-06 19:00:21
【问题描述】:

我有执行 shell 命令的代码:

public void ExecuteShellCommand(string _FileToExecute, string _CommandLine, ref string _outputMessage, ref string _errorMessage)
    {
        //Set process variable.
        //Provides access to local and remote processes and enables you to start and stop local system processes.
        System.Diagnostics.Process _Process = null;
        try
        {
            _Process = new System.Diagnostics.Process();
            _Process.StartInfo.Verb = "runas";

            //Invokes the cmd process specifying the command to be executed.

            var culture = new System.Globalization.CultureInfo("pt-BR", true);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR", false);

            string _CMDProcess = string.Format(culture, @"{0}\cmd.exe",
                                               new object[] { Environment.SystemDirectory });

            //Pass executing file to cmd (Windows command interpreter) as a arguments
            // /C tells cmd we want it to execute the comand that follows, then exit.
            string _Arguments = string.Format(culture, "/C {0}",
                                              new object[] { _FileToExecute });

            //Pass any command line parameters for execution
            if (!string.IsNullOrEmpty(_CommandLine))
            {
                _Arguments += string.Format(culture, " {0}",
                                            new object[] { _CommandLine, culture });
            }

            var _ProcessStartInfo =
                new System.Diagnostics.ProcessStartInfo(_CMDProcess, _Arguments);

            //Sets a value indicating not to start the process in a new window. 
            _ProcessStartInfo.CreateNoWindow = true;

            //Sets a value indicating now to use the operating system shell to start the process.
            _ProcessStartInfo.UseShellExecute = false;

            //Sets the value that indicates the output/input/error of an aplication is written to the Process.
            _ProcessStartInfo.RedirectStandardOutput = true;
            _ProcessStartInfo.RedirectStandardInput = true;
            _ProcessStartInfo.RedirectStandardError = true;
            _Process.StartInfo = _ProcessStartInfo;

            //Starts a process resource and associates it with a Process component.
            _Process.Start();

            //Instructs the Process component t wait indefitely for the associated process to exit.
            _errorMessage = _Process.StandardError.ReadToEnd();
            _Process.WaitForExit();

            //Instructs the Process component to wait indefinitely for the associated process to exit.

            _outputMessage = _Process.StandardOutput.ReadToEnd();
            _Process.WaitForExit();
        }
        catch (Win32Exception _Win32Exception)
        {
            //Error
            MessageBox.Show("Win32 Exception caught in process: " + _Win32Exception.ToString());
        }
        catch (Exception _Exception)
        {
            //Error
            MessageBox.Show("Exception caught in process: " + _Exception.ToString());
        }
        finally
        {
            _Process.Close();
            _Process.Dispose();
            _Process = null;
        }
    }

问题是我的系统语言是pt-BR,输出:

_outputMessage = _Process.StandardOutput.ReadToEnd();

返回损坏的字符串:

返回字符串:“Autentica‡Æo

预期字符串:“Autenticação

但是如果我在 CMD 中使用相同的命令,一切都会返回正常,没有错误或损坏的字符串...

我的代码有什么问题?


编辑:

我正在尝试通过代码执行 shell 命令。使用 cmd.exe + 参数。


工作:

_ProcessStartInfo.StandardOutputEncoding = Encoding.GetEncoding(850);

现在,编码匹配。

【问题讨论】:

  • 子进程是否设置了控制台编码?
  • 我什么都没做,输出使用System.Text.SBCSCodePageEncoding...
  • SBCS 代表 单字节字符集,在这种情况下,像 çã 这样的组合字符会严重损坏。
  • 我正在尝试使用:_ProcessStartInfo.StandardOutputEncoding = new System.Text.ASCIIEncoding(); 或 UTF-8、UTF-7、UTF32,没有任何效果...
  • @Mgetz 好吧,感谢您的帮助。我要把这件事放下一段时间。

标签: c# wpf shell process


【解决方案1】:

它是code page 850,葡萄牙语的 MS-Dos 代码页。 ç = 0x87, ã = 0xc6。
您的程序当前错误地使用了code page 1252,0x87 = ‡,0xc6 = Æ。

【讨论】:

猜你喜欢
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多