System.Diagnostics.Process.StandardOutput返回带有重音的错误字符串

本文关键字:错误 字符串 Diagnostics Process StandardOutput 返回 System | 更新日期: 2023-09-27 18:21:13

我有执行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.Diagnostics.Process.StandardOutput返回带有重音的错误字符串

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