この記事は翻訳者によって翻訳されたものです。 記事の文章にポインターを重ねると、原文のテキストが表示されます。
訳文
原文
このトピックはまだ評価されていません - このトピックを評価する

Application.LocalUserAppDataPath プロパティ

ローカルで非ローミング ユーザーのアプリケーション データのパスを取得します。

名前空間:  System.Windows.Forms
アセンブリ:  System.Windows.Forms (System.Windows.Forms.dll 内)
public static string LocalUserAppDataPath { get; }

プロパティ値

型: System.String
ローカルで非ローミング ユーザーのアプリケーション データのパス。

ローカル ユーザーとは、ユーザー プロファイルが格納されているシステムにログオンしたユーザーのことです。 パスが存在しない場合は、次の形式で作成されます。

Base Path\CompanyName\ProductName\ProductVersion

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

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

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

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


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 {}
}


.NET Framework

サポート対象: 4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

サポート対象: 4、3.5 SP1

Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2

.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
この情報は役に立ちましたか。
(残り 1500 文字)
コミュニティ コンテンツ 追加
注釈 FAQ