简体   繁体   English

如何创建自然的控制台暂停

[英]How to create a natural console pause

How do you make C# do the actual "Press any key to continue" thing? 您如何使C#实际执行“按任意键继续”操作? Like when you type pause in cmd. 就像在cmd中键入pause一样。

I saw an example once if some special command in C# which did exactly this. 我曾经看到一个例子,如果C#中有一些特殊的命令可以做到这一点。 I have tried to find it again but every other example on the internet is the obvious Console.ReadKey(); 我试图再次找到它,但互联网上的所有其他示例都是显而易见的Console.ReadKey(); perhaps with a Console.WriteLine("Press any key to continue . . ."); 也许使用Console.WriteLine("Press any key to continue . . .");

Does anyone know of what I mean? 有人知道我的意思吗?

You remember wrong: there is no special command in c# that automatically print the text and wait for input. 您记得错了:c#中没有特殊的命令可以自动打印文本并等待输入。

Anyway you could write a method: 无论如何,您可以编写一个方法:

public static void Pause()
{
    Console.Write("Press any key to continue ...");
    Console.ReadKey(true);
}

As far as this discussion goes, there is no built-in support for that. 就此讨论而言,没有内置的支持。

You will have to write something similar to this: 您将必须编写类似以下内容:

Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);

You can also import a C function if you prefer. 如果愿意,也可以导入C函数 The below is citated from that link. 该链接引用了以下内容。

You have to pinvoke this C function into your C# application. 您必须将此C函数添加到C#应用程序中。 Luckily, this process is very easy. 幸运的是,此过程非常简单。 There are a few steps: 有几个步骤:

1) Insert System.Runtime.InteropServices to your using clauses. 1)将System.Runtime.InteropServices插入到using子句中。 2) Insert this line in your class (usually in the first few lines) 2)在您的班级中插入此行(通常在前几行中)

[DllImport("msvcrt.dll")] static extern bool system(string str); [DllImport(“ msvcrt.dll”)]静态外部布尔系统(字符串str);

3) In that same class, simply write this: system("pause"); 3)在同一类中,只需编写以下代码:system(“ pause”);

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

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