Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");
startInfo.Arguments = "/c " + URL;
Process p = new Process();
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
p = Process.Start(startInfo);
string original = p.StandardOutput.ReadToEnd();
string result1 = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(original));
string result2 = Encoding.BigEndianUnicode.GetString(Encoding.BigEndianUnicode.GetBytes(original));
string result3 = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(original));
string result4 = Encoding.UTF32.GetString(Encoding.UTF32.GetBytes(original));
string result5 = Encoding.UTF7.GetString(Encoding.UTF7.GetBytes(original));
string result6 = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(original));

cmd output contains russian letters, that can't be decoded properly with all encodings what I tried. Please help.

EDIT:
I tried also
C#
startInfo.StandardOutputEncoding = Encoding."all possible encodings";
- not helped.
Posted
Updated 28-May-13 9:09am
v3
Comments
Sergey Alexandrovich Kryukov 28-May-13 13:58pm    
Look at what you are doing!

You are not trans-coding anything. If you use any of UTFs, you simply get exactly the same as original string. Your "original" is already a string! And it should already be the right string, check it up. More generally, you need to read raw bytes (not characters!) and then encode then assuming some encoding. Encoding make a string our of array of byte and visa versa, not string from string.

—SA
MegaSinner 28-May-13 14:02pm    
I don't know which encoding to use, how to convert the string?
Dave Kreskowiak 28-May-13 14:13pm    
To what?? Another string?? You can't! Converting the string you get from the console to an array of bytes and applying another encoding to it will NOT get you anything other than the original string back if not a pile of unintelligeble garbage.
MegaSinner 28-May-13 14:25pm    
To see russian letters in output as I see it in cmd.exe.
Dave Kreskowiak 28-May-13 15:12pm    
The string you get IS decoded correctly. You're getting whatever the console gets.

1 solution

OK, this is not so simple. However, please read my comments to the question first.

First, you need to set a correct font for your console. Run the application with some pause (see the second code sample below) once, click "Properties" and change the font.

Now, you need to set the encoding for three things: in first application (to be executed as a child process), this is the console output encoding. In the parent-process application, you need to do the same if you want to see the result, you need to do the same, but you also need to set standard output encoding in System.Diagnostics.ProcessStartInfo. Also, if you use some input, you should need all three things in input.

On my system, all UTFs except UTF-8, throw an exception. Let it be: only one UTF is probably currently implemented (Windows 7 Pro, in my case).

First, let's see how can you write the application which simply outputs the Unicode text:
C#
namespace WriteAndPresentUnicode {
    using System;

    class Program {
        static void Main(string[] args) {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("Пишем по-русски..."); // "We are writing in Russian..."
        } //Main
    } //class Program

} //namespace WriteAndPresentUnicode


Now, let's see how to start it as a chile process with redirection of output:
C#
namespace ReadUnicode {
    using System;
    using System.Diagnostics;
    using System.IO;

    class Program {
        
        const string application = "WriteAndPresentUnicode.exe";
        
        static void Main(string[] args) {
            Process myProcess = new Process();
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(application);
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8; // this is the most important part, to get correct myString, see below
            myProcess.StartInfo = myProcessStartInfo;
            myProcess.Start();
            StreamReader myStreamReader = myProcess.StandardOutput;
            string myString = myStreamReader.ReadToEnd();
            myProcess.WaitForExit();
            myProcess.Close();
            Console.InputEncoding = System.Text.Encoding.UTF8;
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine(myString);
            Console.WriteLine("Press any key...");
            Console.ReadKey(true);
        } //Main

    } //class Program

} //namespace ReadUnicode


That's it, tested.

—SA
 
Share this answer
 
v2
Comments
MegaSinner 29-May-13 12:15pm    
Thanks, but it helpless in my case: output can't be decoded correctly from russian '.../system32/rasdial.exe'
I have attached rasdial.exe here, please try it:
http://www.sendspace.com/file/if9yl1
Sergey Alexandrovich Kryukov 29-May-13 12:45pm    
It can be! Check up again. If could be just presentation. Sorry, I won't execute attached EXE file. If it shows Russian, the method I explained will work for you, I'll guarantee that.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900