0

シェル コマンドを実行する次のコードがあります。

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内で同じコマンドを使用すると、すべてが正常に返され、エラーや壊れた文字列は返されません...

コードの何が問題になっていますか?


編集:

コードを介してシェルコマンドを実行しようとしています。cmd.exe + 引数を使用します。


働く:

_ProcessStartInfo.StandardOutputEncoding = Encoding.GetEncoding(850);

これで、エンコーディングが一致しました。

4

1 に答える 1

1

これはコード ページ 850で、ポルトガル語の MS-Dos コード ページです。ç = 0x87、ã = 0xc6。
あなたのプログラムは現在、コード ページ1252、0x87 = ‡、0xc6 = Æ を誤って使用しています。

于 2013-07-06T21:46:20.473 に答える