Application

プロパティ

プロパティ 内容
string CommonAppDataPath すべてのユーザーが共有するアプリケーション データのパス
string UserAppDataPath ローミング ユーザーのアプリケーション データのパス
string LocalUserAppDataPath 非ローミング ユーザーのアプリケーション データのパス
string ExecutablePath アプリケーションを開始した実行可能ファイルの、ファイル名を含めたパス
string StartupPath アプリケーションを開始した実行可能ファイルの、ファイル名を含めないパス
     
プロパティ - Application クラス (System.Windows.Forms) | Microsoft Learn

メソッド

  機能
Run() 現在のスレッドで、標準のアプリケーション メッセージ ループを実行を開始する
Restart() アプリケーションを終了し、新しいインスタンスを開始する。つまり再起動する
Exit() 終了しなければならないメッセージ ポンプを通知し、メッセージが処理されるとアプリケーションを閉じる
DoEvents() メッセージループに現在ある、すべてのWindowsメッセージを処理する
AddMessageFilter(IMessageFilter) メッセージ フィルタを追加し、Windowsメッセージを監視する
EnableVisualStyles() アプリケーションの視覚スタイルを有効にする
SetCompatibleTextRenderingDefault(Boolean) コントロールのUseCompatibleTextRenderingプロパティの既定値を指定する。trueならばGraphicsクラス ベースのGDI+を、さもなくばTextRendererクラス ベースのGDIを用いる
 
メソッド - Application クラス (System.Windows.Forms) | Microsoft Learn

Run()

public static void Run()
Application.Run メソッド (System.Windows.Forms) | MSDN

Restart()

アプリケーションを再起動できます。

[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void Restart()
Application.Restart メソッド (System.Windows.Forms) | Microsoft Learn

通常のアプリケーションではExit()と共通の処理で終了し、新しいインスタンスはProcess.Start()で開始されます。またコマンドライン引数も引き継がれます。Restart - Application.cs

終了方法がExit()と共通であり異常終了と区別できないため、終了の理由によって処理を分けているならば他の方法で終了させ、Process.Start()で開始するようにします。

Exit()

アプリケーションを終了できます。

public static void Exit()
Application.Exit メソッド (System.Windows.Forms) | MSDN

このメソッドを呼び出したことでFormアプリケーションが終了するとき、FormClosingでのCloseReasonはApplicationExitCallであり、例外を捕捉しなかったことで異常終了するときと同じです。

DoEvents()

public static void DoEvents()
Application.DoEvents Method (System.Windows.Forms) | Microsoft Learn

このメソッドを呼び出すとすべてのWindowsメッセージが処理されるまで、現在のスレッドは停止します。

メッセージによってイベントが引き起こされることでアプリケーションの他のコードが実行されることがあり、デバッグの困難な予期せぬ動作をすることがあります。よって長い時間の処理をするには、スレッドを用いるようにします。Remarks - Application.DoEvents Method (System.Windows.Forms) | Microsoft Learn

AddMessageFilter()

メッセージ フィルタを追加し、Windowsメッセージを監視できます。

public static void AddMessageFilter(
    IMessageFilter value // IMessageFilterを実装したインスタンス
)
Application.AddMessageFilter メソッド (IMessageFilter) (System.Windows.Forms) | MSDN

実際にWindowsメッセージを受け取るにはIMessageFilterを継承したクラスを作成し、PreFilterMessage()を定義します。

class MyClass : IMessageFilter
{
    public void MyClass()
    {
        Application.AddMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message m)
    {
        const int WM_KEYDOWN = 0x100;
        if (m.Msg == WM_KEYDOWN)
        {
            Keys keyCode = (Keys)m.WParam & Keys.KeyCode;
            if (keyCode == Keys.Escape)
            {
                //
                return true;
            }
        }
        return false;
    }
}

追加したメッセージ フィルタは、RemoveMessageFilter()で除去できます。

EnableVisualStyles()

public static void EnableVisualStyles ();
Application.EnableVisualStyles メソッド (System.Windows.Forms) | Microsoft Learn

このメソッドはアプリケーションのコントロールを作成する前に呼ばねばならず、一般的にMain()関数の最初に記述します。

イベント

イベント 発生タイミング
Idle アプリケーションが処理を終了し、アイドル状態に入るとき
ThreadException 捕捉されないスレッド例外が発生したとき
   

ThreadException

Application.ThreadException イベント (System.Windows.Forms) | MSDN
Microsoft Learnから検索