How to avoid C# console applications from closing automatically.

  • One way is to interop it with msvcrt.dll
    You can pinvoke this C function into your C# application. 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");
  • The simplest way that I always use is to ask for input

    Console.ReadLine();

    or

    Console.ReadKey();

    ReadLine() waits for , ReadKey() waits for any key (except for modifier keys).

posted @ 2015-03-01 15:22  孤单昊子  阅读(161)  评论(0编辑  收藏  举报