Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.Diagnostics.Process.StandardOutput returning bad string that have accentuation
    primarykey
    data
    text
    <p>I have this code that execute shell commands:</p> <pre><code>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; } } </code></pre> <p>The problem is that my system language is <code>pt-BR</code>, the output: </p> <pre><code>_outputMessage = _Process.StandardOutput.ReadToEnd(); </code></pre> <p>returns broken strings:</p> <p>Returned string: "<code>Autentica‡Æo</code>"</p> <p>Expected string: "<code>Autenticação</code>" </p> <p>But if I use the same command inside CMD, everything returns okay, no erros or broken strings...</p> <p><strong>What is wrong with my code?</strong></p> <hr> <p><strong>EDIT:</strong></p> <p>I'm trying execute shell commands via code. Using cmd.exe + arguments.</p> <hr> <p><strong>Working:</strong></p> <pre><code>_ProcessStartInfo.StandardOutputEncoding = Encoding.GetEncoding(850); </code></pre> <p>Now, the encoding matches.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload