1

I am starting a cmd.exe from my c# application and redirect its inputstream.This works fine for normal chars like "abc" But when i try to redirect chars like "äöüßáàâ" in the consolewindow appears "õ÷³óô".

Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/K";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;

myProcess.StartInfo.WorkingDirectory = @"c:\";
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.WriteLine("äöüßáàâ");

myStreamWriter.Encoding says its encoding is codepage 1252 i tryed to convert my string into it but it didnt change the result. How to convert my string that it is shown correct?

  • 1
    It just worked without a problem for me. Copy pasted Your code, and it works (win 7). – ntohl Aug 11 '15 at 11:16
  • i am on a windows 7 pc to, i will try it on another machine – user3289727 Aug 11 '15 at 11:19
  • ok i tryed it from a console application and it works as you said. Unfortunately my programm is a windows forms application and their it fails :( – user3289727 Aug 11 '15 at 11:23
  • Must admit, the code didnt work for me under visual studio, the cmd window didnt stay open at all. I got the same as the OP when running the winforms app from command line. – BugFinder Aug 11 '15 at 11:40
  • Yeah I'm struggling with keeping the cmd open also. How did You have a graphical interface? I tried to`myProcess.WaitForInputIdle();` at the end of the code, but it missed the graphical interface. – ntohl Aug 11 '15 at 11:45
  • the myProcess.StartInfo.Arguments = "/K"; keeps the window open for me – user3289727 Aug 11 '15 at 12:12
  • the /k does as long as you arent in debug mode.. if its in debug mode the window doesnt stay open. – BugFinder Aug 11 '15 at 12:21
  • my cmd window stays open even in debug mode – user3289727 Aug 11 '15 at 12:33
  • @BugFinder for me it doesn't stay open either way. – ntohl Aug 12 '15 at 12:32
  • Which raises the odd question of why does it act differently considering /k is supposed to keep it open – BugFinder Aug 12 '15 at 12:45

1 Answers1

0

I took the code and ran the same.. When I didnt redirect the output, I saw the same as the OP. However

Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/K";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
//myProcess.StartInfo.StandardOutputEncoding = Encoding.UTF32;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.WorkingDirectory = @"c:\";
myProcess.Start();

StreamWriter myStreamWriter = myProcess.StandardInput;
StreamReader myStreamReader = myProcess.StandardOutput;
myStreamWriter.WriteLine("äöüßáàâ");
richTextBox1.Text = myStreamReader.ReadToEnd();

This produced c:\>äöüßáàâ as expected in the text box.. even though it didnt seem to show it as right in the console window that showed.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • When i copy paste out of the cmd window into notepad the string is also ok. Its only in the cmd window where its displayed wrong but in the background the data seems to be valid at least after copy and paste through the clipboard. So it seems logic that it is shown in the richtextbox – user3289727 Aug 11 '15 at 12:18
  • Hmm, you didnt mention that. – BugFinder Aug 11 '15 at 12:22
  • Have you read http://stackoverflow.com/questions/2855675/process-standardinput-encoding-problem – BugFinder Aug 11 '15 at 12:49
  • Many thanks this i think helps a lot. The code in the 2nd post their did not help but i got an idea. I used a for loop to iterate between different encodings and StreamWriter Writer = new StreamWriter(myProcess.StandardInput.BaseStream, Encoding.GetEncoding(i)); for i= 850,857,858,861,865 did all look good. I will take a look where the differences are between this codepages but i think i am able to solve the rest alone from here. Thanks for your help and shame on me my search didnt find that post. – user3289727 Aug 11 '15 at 13:10