Anon343224user
Anon343224user

Reputation: 614

How to create a natural console pause

How do you make C# do the actual "Press any key to continue" thing? Like when you type pause in cmd.

I saw an example once if some special command in C# which did exactly this. I have tried to find it again but every other example on the internet is the obvious Console.ReadKey(); perhaps with a Console.WriteLine("Press any key to continue . . .");

Does anyone know of what I mean?

Upvotes: 2

Views: 465

Answers (2)

Jim Aho
Jim Aho

Reputation: 11957

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. The below is citated from that link.

You have to pinvoke this C function into your C# application. Luckily, this process is very easy. There are a few steps:

1) Insert System.Runtime.InteropServices to your using clauses. 2) Insert this line in your class (usually in the first few lines)

[DllImport("msvcrt.dll")] static extern bool system(string str);

3) In that same class, simply write this: system("pause");

Upvotes: 3

giammin
giammin

Reputation: 18968

You remember wrong: there is no special command in c# that automatically print the text and wait for input.

Anyway you could write a method:

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

Upvotes: 3

Related Questions