问题
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.
I tried:
startInfo.StandardOutputEncoding = Encoding."all possible encodings";
but no help.
any ideas?
回答1:
Old question, but no possible correct answer.
Here it is:
process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(850);
850 is the standart cmd-page. So the user which is using the application will get the characters just as he would expect when using the command line itself.
This one solved all symbol-problems on a german OS for me.
OEM code pages:
437 (US)
720 (Arabic)
737 (Greek)
775 (Baltic)
850 (Multilingual Latin I)
852 (Latin II)
855 (Cyrillic)
857 (Turkish)
858 (Multilingual Latin I + Euro)
862 (Hebrew)
866 (Russian)
回答2:
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:
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:
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
-Tested-
回答3:
This works for me:
startInfo.StandardOutputEncoding = Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage)
回答4:
The solution to use UTF8 encoding to write console is good if console application is yours. But if you using standart Windows utilites like 'netsh' it doesn't help.
Standart cmd code pages is OEM.
For example for Russian language:
process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(866);
process.StartInfo.StandardErrorEncoding = Encoding.GetEncoding(866);
OEM code pages:
437 (US)
720 (Arabic)
737 (Greek)
775 (Baltic)
850 (Multilingual Latin I)
852 (Latin II)
855 (Cyrillic)
857 (Turkish)
858 (Multilingual Latin I + Euro)
862 (Hebrew)
866 (Russian)
回答5:
Try this if nothing else helps!
After a lot of trying & suffering, here's what I found out: no matter how UTF8 your output stream is, no matter how you set 65001, things will not work until you start a child process inside the child process. Sounds silly but true.
Look:
chcp 65001 && dir // nope, still 437
chcp 65001 && cmd /c dir // correct UTF8
Hope this saves you at least as much time as it took from my miserable life!
来源:https://stackoverflow.com/questions/16803748/how-to-decode-cmd-output-correctly