2

I am writing a windows forms application in C# I have a Process Object which runs a cmd command and returns it's output.

Process Pro = new Process();
Pro.StartInfo.FileName = "cmd.exe";
Pro.StartInfo.Arguments = "<Dos Command here>";
Pro.StartInfo.CreateNoWindow = true;
Pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Pro.StartInfo.RedirectStandardOutput = true;
Pro.StartInfo.UseShellExecute = false;
Pro.Start();

Which works fine! However if the output of the command is not ASCII(in my case Greek), the Output are random symbols. Surely an encoding problem. If i run the same code on a console application everything runs smoothly.

I tried reading the Base stream as UTF-8, but no luck!

System.IO.StreamReader Rdr = new System.IO.StreamReader(Pro.StandardOutput.BaseStream, Encoding.UTF8);

Is there any way to read the output properly in a winform application? Thnx!

user969245
  • 83
  • 1
  • 1
  • 8
  • Perhaps [this post](http://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using) can help? – Daniel B Sep 28 '11 at 14:34
  • Assign the StartInfo.StandardOutputEncoding property. To what encoding is unguessable. Maybe 737, the old DOS code page for Greek. – Hans Passant Sep 28 '11 at 15:15

1 Answers1

0

The real solution is base on this: unicode-characters-in-windows-command-line-how check here: Wiki code page for the code page you need.

you can also do an ugly hack, writing the command to a batch file (f.e foo.bat) then running it as foo.bat > log.txt then you can read the output from log.txt.

Community
  • 1
  • 1
Nadav Ben-Gal
  • 549
  • 3
  • 13