1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studioメモ キー入力待ち

Last updated at Posted at 2016-11-22

概要

コンソールアプリを「デバッグの開始」時にキー入力待ちする方法。

コンソールアプリをVisual Studio上で実行するとき、デバッグなしで開始(Ctrl+F5)だとコンソールがキー入力待ちになり、出力内容を確認できます。
しかし、デバッグの開始(F5)だとコンソールがすぐに閉じてしまうため、出力内容を確認できません。
デバッグの開始(F5)時でもキー入力待ちをする方法を紹介します。

コード

Debugger.IsAttachedプロパティがTrueの時にキー入力待ちのコードを実行するようにします。

using System;

namespace ConsoleApplication001
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");

            // デバッガーがプロセスにアタッチされているか?
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("続行するには何かキーを押してください . . .");
                Console.ReadKey();
            }
        }
    }
}
実行結果
hello
続行するには何かキーを押してください . . .

参考サイト

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?