2011-11-17 67 views
11

只是碰到了下面描述的問題。如果「Console.TreatControlCAsInput = true;」,則必須在ReadLine()上按[enter]兩次。TreatControlCAsInput問題。這是一個錯誤?

我已經在下面寫了一些演示代碼。我正確地猜測這段代碼在.NET 4框架中演示了一個錯誤?

 Console.Write("Test 1: Console.TreatControlCAsInput = false\nType \"hello\": "); 
     { 
      string readline = Console.ReadLine(); // type "hello" [enter]. 
      Console.WriteLine("You typed: {0}", readline); 
      // Prints "hello". 
     } 

     Console.Write("Test 2: Console.TreatControlCAsInput = true\nType \"hello\": "); 
     Console.TreatControlCAsInput = true; 
     { 
      string readline = Console.ReadLine(); // type "hello" [enter]. 
      Console.WriteLine("You typed: {0}", readline); 
      // Should print "hello" - but instead, you have to press [enter] 
      // *twice* to complete the ReadLine() command, and it adds a "\r" 
      // rather than a "\n" to the output (so it overwrites the original line) 
     } 

     // This bug is a fatal error, because it makes all ReadLine() commands unusable. 

     Console.Write("[any key to exit]"); 
     Console.ReadKey(); 
+0

你運行這個什麼平臺上? – Oded

+1

我可以在Windows 7上重現這一點。回車似乎正在做** HOME **按鈕的功能 –

+1

我在2006年看到它在報告中標記爲關閉的設計。 http://connect.microsoft.com/VisualStudio/feedback/details/226101/console-treatcontrolcasinput-true-breaks-readline – automatic

回答

13

它與Windows控制檯子系統known issue並早在2006年

發佈Microsoft在22/05/2007在12:37

你好阿羅斯有報道微軟連接,感謝您在System.Console中報告此問題。 行爲存在於Windows控制檯子系統中,如演示 和附帶的Win32 C應用程序一樣。我已將此問題報告給Windows控制檯子系統擁有者 。

謝謝,喬希

+2

作爲一個方面說明:該連接項目被關閉爲「按設計」。所以聽起來他們認爲這是一個功能。 – NotMe

+0

@Chris Lively驚人的「功能」 - 它打破了ReadLine(),使其無法使用。我想知道有多少開發者與這個特殊問題相沖突。 – Contango