Application.UserAppDataPath Właściwość

Definicja

Pobiera ścieżkę dla danych aplikacji użytkownika.

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

Wartość właściwości

String

Ścieżka danych aplikacji użytkownika.

Przykłady

Poniższy przykład kodu wyświetla dwie formularze i zamyka aplikację po zamknięciu obu formularzy. Po uruchomieniu i zakończeniu działania aplikacji zostanie zapamiętana pozycja każdego formularza. W tym przykładzie UserAppDataPath pokazano użycie właściwości do przechowywania danych aplikacji dla użytkownika.

Klasa MyApplicationContext dziedziczy i ApplicationContext śledzi, kiedy każdy formularz jest zamknięty, i zamyka bieżący wątek, gdy są. Klasa przechowuje pozycje każdego formularza dla użytkownika. Dane położenia formularza są przechowywane w pliku o nazwie Appdata.txt , który jest tworzony w lokalizacji określonej przez UserAppDataPath. Metoda Main wywołuje metodę Application.Run(context) , aby uruchomić aplikację przy użyciu elementu ApplicationContext.

Ten kod jest fragmentem przykładu pokazanego w omówieniu ApplicationContext klasy. Niektóre kody nie są wyświetlane w celu zwięzłości. Zobacz ApplicationContext , aby zapoznać się z całą listą kodu.

   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

Uwagi

Jeśli ścieżka nie istnieje, zostanie utworzona w następującym formacie:

Ścieżka podstawowa\CompanyName\ProductName\ProductVersion

Dane przechowywane w tej ścieżce są częścią profilu użytkownika, który jest włączony na potrzeby roamingu. Użytkownik mobilny działa na więcej niż jednym komputerze w sieci. Profil użytkownika dla użytkownika mobilnego jest przechowywany na serwerze w sieci i jest ładowany do systemu po zalogowaniu się użytkownika. Aby profil użytkownika był brany pod uwagę w przypadku roamingu, system operacyjny musi obsługiwać profile mobilne i musi być włączony.

Typowa ścieżka podstawowa to C:\Documents i Ustawienia\ username\Application Data. Ta ścieżka będzie jednak inna, jeśli aplikacja Windows Forms zostanie wdrożona przy użyciu ClickOnce. ClickOnce tworzy własny katalog danych aplikacji, który jest odizolowany od wszystkich innych aplikacji. Aby uzyskać więcej informacji, zobacz Uzyskiwanie dostępu do danych lokalnych i zdalnych w aplikacjach ClickOnce.

Dotyczy

Zobacz też