Application.UserAppDataPath プロパティ

定義

ユーザーのアプリケーション データのパスを取得します。

C#
public static string UserAppDataPath { get; }

プロパティ値

ユーザーのアプリケーション データのパス。

次のコード例では、2 つのフォームを表示し、両方のフォームが閉じられたときにアプリケーションを終了します。 アプリケーションが開始および終了すると、各フォームの位置が記憶されます。 この例では、 プロパティを使用してユーザーの UserAppDataPath アプリケーション データを格納する方法を示します。

クラス MyApplicationContext は から ApplicationContext 継承され、各フォームが閉じられると追跡され、両方が閉じられると現在のスレッドが終了します。 クラスには、ユーザーの各フォームの位置が格納されます。 フォーム位置データは、 によって決定されたUserAppDataPath場所に作成されるAppdata.txtというタイトルのファイルに格納されます。 メソッドは Main を呼び出 Application.Run(context) して、 を指定してアプリケーションを起動します ApplicationContext

このコードは、クラスの概要に ApplicationContext 示されている例からの抜粋です。 簡潔にするために一部のコードは表示されません。 コードの一覧全体については、 を参照してください ApplicationContext

C#
private MyApplicationContext()
{
    _formCount = 0;

    // Handle the ApplicationExit event to know when the application is exiting.
    Application.ApplicationExit += new EventHandler(this.OnApplicationExit);

    try
    {
        // Create a file that the application will store user specific data in.
        _userData = new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate);
    }
    catch (IOException e)
    {
        // Inform the user that an error occurred.
        MessageBox.Show("An error occurred while attempting to show the application." +
                        "The error is:" + e.ToString());

        // Exit the current thread instead of showing the windows.
        ExitThread();
    }

    // Create both application forms and handle the Closed event
    // to know when both forms are closed.
    _form1 = new AppForm1();
    _form1.Closed += new EventHandler(OnFormClosed);
    _form1.Closing += new CancelEventHandler(OnFormClosing);
    _formCount++;

    _form2 = new AppForm2();
    _form2.Closed += new EventHandler(OnFormClosed);
    _form2.Closing += new CancelEventHandler(OnFormClosing);
    _formCount++;

    // Get the form positions based upon the user specific data.
    if (ReadFormDataFromFile())
    {
        // If the data was read from the file, set the form
        // positions manually.
        _form1.StartPosition = FormStartPosition.Manual;
        _form2.StartPosition = FormStartPosition.Manual;

        _form1.Bounds = _form1Position;
        _form2.Bounds = _form2Position;
    }

    // Show both forms.
    _form1.Show();
    _form2.Show();
}

private void OnApplicationExit(object sender, EventArgs e)
{
    // When the application is exiting, write the application data to the
    // user file and close it.
    WriteFormDataToFile();

    try
    {
        // Ignore any errors that might occur while closing the file handle.
        _userData.Close();
    }
    catch { }
}

注釈

パスが存在しない場合は、次の形式で作成されます。

ベース パス\CompanyName\ProductName\ProductVersion

このパスに格納されているデータは、ローミングが有効になっているユーザー プロファイルの一部です。 ローミング ユーザーは、ネットワーク内の複数のコンピューターで動作します。 ローミング ユーザーのユーザー プロファイルは、ネットワーク上のサーバーに保持され、ユーザーがログオンしたときにシステムに読み込まれます。 ユーザー プロファイルをローミングと見なすには、オペレーティング システムがローミング プロファイルをサポートし、有効にする必要があります。

一般的な基本パスは C:\Documents and Settings\username\Application Data です。 ただし、Windows フォーム アプリケーションが ClickOnce を使用して配置されている場合、このパスは異なります。 ClickOnce は、他のすべてのアプリケーションから分離された独自のアプリケーション データ ディレクトリを作成します。 詳細については、ローカルへのアクセスとリモート データには、ClickOnce アプリケーション を参照してください。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください