简体   繁体   English

使用时调用C#ReadKey崩溃控制台应用程序

[英]C# ReadKey crashing console app when called using <nul

So I decided to start programming in C#, and one of the things I did was to create a "pausec.exe" (a pause.exe clone). 所以我决定开始用C#编程,我做的一件事就是创建一个“pausec.exe”(pause.exe克隆)。 It works, but when calling it like: 它有效,但在调用它时:

< nul pausec  

...it crashes. ......它崩溃了 The error I get - translated from Spanish to the best of my knowledge - goes like this: 我得到的错误 - 从西班牙语翻译到我所知的最佳 - 是这样的:

Unhandled exception: System.InvalidOperationException: Can't read keys when any of the applications doesn't have a console or when the console input has been redirected from a file. 未处理的异常:System.InvalidOperationException:当任何应用程序没有控制台或控制台输入已从文件重定向时,无法读取键。 Try with Console.Read. 尝试使用Console.Read。

And the stacktrace telling me where the error is: stacktrace告诉我错误在哪里:

 in System.Console.ReadKey(Boolean intercept)  
 in System.Console.ReadKey()  
 in pausec.Program.Main(String[] args)

This is the code I'm running: 这是我正在运行的代码:

using System;

namespace pausec
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
            Console.Write("\n");
        }
    }
}

I'm wondering if there's a workaround for this, maybe even a way to ignore the ReadKey when using < nul ? 我想知道是否有一个解决方法,甚至可以在使用< nul时忽略ReadKey

Any help is appreciated. 任何帮助表示赞赏。
Thanks in advance 提前致谢

UPDATE : Found a way, by removing the intercept ( as Alberto Solano suggested ) and then, adding Console.Write("\\b \\b"); 更新 :找到一种方法,通过删除拦截( 如Alberto Solano建议的那样 ),然后添加Console.Write("\\b \\b"); after the ReadKey method, it works. ReadKey方法之后,它可以工作。 Weird thing is, when I copy the application to my desktop, it doesn't wait for user input and closes automatically. 奇怪的是,当我将应用程序复制到我的桌面时,它不会等待用户输入并自动关闭。

UPDATE 2 : It works perfectly now. 更新2 :现在完美运作。 Thank you all for answering! 谢谢大家回答!

控制台有一个方法,您可以检查是否已重定向stdin。

public static bool IsInputRedirected { get; }

Your program throws that exception because, with Console.ReadKey(true); 您的程序抛出该异常,因为使用Console.ReadKey(true); , as stated in the MSDN documentation : ,如MSDN文档中所述

If the intercept parameter is true, the pressed key is intercepted and not displayed in the console window; 如果截取参数为真,则截取按下的键,不显示在控制台窗口中; otherwise, the pressed key is displayed. 否则,显示按下的键。

You're not reading or "listening" to any key pressed in the keyboard, and then there's no key to intercept and to not display in the console window. 你没有阅读或“聆听”键盘上按下的任何键,然后没有键来拦截和不显示在控制台窗口中。

If you want just to press any key to close the program, use: 如果您只想按任意键关闭程序,请使用:

Console.ReadKey(); //this won't intercept any key pressed

or 要么

Console.ReadLine();

UPDATE: You asked in the comment how to hide the key pressed by the user. 更新:您在评论中询问如何隐藏用户按下的键。 This code should do the trick: 这段代码可以解决这个问题:

ConsoleKeyInfo cki;
Console.Write("Press any key to continue . . . ");
cki = Console.ReadKey(true);

I have faced the same error. 我遇到了同样的错误。

So i just checked my project Output Type. 所以我刚检查了我的项目输出类型。

ProjectName->RightClick->Properties ProjectName-> RightClick->属性

there you can see your output type. 在那里你可以看到你的输出类型。

So i change that into Console application because in my case it was seems to be windows application before. 所以我将其更改为控制台应用程序,因为在我的情况下它似乎是以前的Windows应用程序。 Then its works fine. 然后它的工作正常。

/// <summary>
/// Written by fredrik92 (http://social.msdn.microsoft.com/Forums/vstudio/en-US/08163199-0a5d-4057-8aa9-3a0a013800c7/how-to-write-a-command-like-pause-to-console?forum=csharpgeneral)
/// Writes a message to the console prompting the user to press a certain key in order to exit the current program.
/// This procedure intercepts all keys that are pressed, so that nothing is displayed in the Console. By default the
/// Enter key is used as the key that has to be pressed.
/// </summary>
/// <param name="key">The key that the Console will wait for. If this parameter is omitted the Enter key is used.</param>    
public static void WriteKeyPressForExit(ConsoleKey key = ConsoleKey.Enter)
{
    Console.WriteLine();
    Console.WriteLine("Press the {0} key on your keyboard to exit . . .", key);
    while (Console.ReadKey(intercept: true).Key != key) { }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM