Application.UserAppDataPath プロパティ

定義

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

public:
 static property System::String ^ UserAppDataPath { System::String ^ get(); };
public static string UserAppDataPath { get; }
static member UserAppDataPath : string
Public Shared ReadOnly Property UserAppDataPath As String

プロパティ値

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

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

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

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

   MyApplicationContext()
   {
      _formCount = 0;
      
      // Handle the ApplicationExit event to know when the application is exiting.
      Application::ApplicationExit += gcnew EventHandler( this, &MyApplicationContext::OnApplicationExit );
      try
      {
         
         // Create a file that the application will store user specific data in.
         _userData = gcnew FileStream( String::Concat( 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: {0}", dynamic_cast<String^>(e) );
         
         // 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 = gcnew AppForm1;
      _form1->Closed += gcnew EventHandler( this, &MyApplicationContext::OnFormClosed );
      _form1->Closing += gcnew CancelEventHandler( this, &MyApplicationContext::OnFormClosing );
      _formCount++;
      _form2 = gcnew AppForm2;
      _form2->Closed += gcnew EventHandler( this, &MyApplicationContext::OnFormClosed );
      _form2->Closing += gcnew CancelEventHandler( this, &MyApplicationContext::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();
   }

   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 ( Exception^ ) 
      {
      }

   }

private:
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 { }
}
Public Sub New()
    MyBase.New()
    _formCount = 0

    ' Handle the ApplicationExit event to know when the application is exiting.
    AddHandler Application.ApplicationExit, AddressOf OnApplicationExit

    Try
        ' Create a file that the application will store user specific data in.
        _userData = New FileStream(Application.UserAppDataPath + "\appdata.txt", FileMode.OpenOrCreate)

    Catch e As IOException
        ' 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()
    End Try

    ' Create both application forms and handle the Closed event
    ' to know when both forms are closed.
    _form1 = New AppForm1()
    AddHandler _form1.Closed, AddressOf OnFormClosed
    AddHandler _form1.Closing, AddressOf OnFormClosing
    _formCount = _formCount + 1

    _form2 = New AppForm2()
    AddHandler _form2.Closed, AddressOf OnFormClosed
    AddHandler _form2.Closing, AddressOf OnFormClosing
    _formCount = _formCount + 1

    ' Get the form positions based upon the user specific data.
    If (ReadFormDataFromFile()) Then
        ' 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
    End If

    ' Show both forms.
    _form1.Show()
    _form2.Show()
End Sub

Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
    ' 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
    End Try
End Sub

注釈

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

ベース パス\CompanyName\ProductName\ProductVersion

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

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

適用対象

こちらもご覧ください