【发布时间】:2013-05-01 05:09:21
【问题描述】:
Console.WriteLine("You have not installed Microsoft SQL Server 2008 R2, do you want to install it now? (Y/N): ");
//var answerKey = Console.ReadKey();
//var answer = answerKey.Key;
var answer = Console.ReadLine();
Console.WriteLine("After key pressed.");
Console.WriteLine("Before checking the pressed key.");
//if(answer == ConsoleKey.N || answer != ConsoleKey.Y)
if (string.IsNullOrEmpty(answer) || string.IsNullOrEmpty(answer.Trim()) || string.Compare(answer.Trim(), "N", true) == 0)
{
Console.WriteLine("The installation can not proceed.");
Console.Read();
return;
}
我已尝试输入这些:
- y -> 它给了我一个空字符串,
- y(whitespace+y) -> 它给了我“y”
我查看了其他类似的帖子,但没有一个能解决我的问题。 ReadLine() 仍然会跳过第一个输入字符。
更新已解决,see below。
【问题讨论】:
-
请查看这篇文章。它将解释您的问题stackoverflow.com/questions/3800343/…
-
我在 Ideone.com 上试过,没有任何问题 (ideone.com/1MSBMb)。您使用的是哪个版本的 .NET?
-
其他读者注意:从下面@Franva的回答来看,问题似乎不完整......
-
@DanPuzey Hi Dan,请指出这个问题遗漏了哪一部分,谢谢
-
@Franva:您没有解释说您在单独的线程中运行代码,这显然是罪魁祸首。没有这些知识的回答永远不会给你任何有用的东西。
标签: c#