0

我有这个执行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);

现在,编码匹配。

4

1 回答 1

1

It is code page 850, the MS-Dos code page for Portuguese. ç = 0x87, ã = 0xc6.
Your program is currently incorrectly using code page 1252, 0x87 = ‡, 0xc6 = Æ.

于 2013-07-06T21:46:20.473 回答