Application.UserAppDataPath Eigenschaft

Definition

Ruft den Pfad für die Anwendungsdaten eines Benutzers ab.Gets the path for the application data of a user.

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

Eigenschaftswert

String

Der Pfad für die Anwendungsdaten eines Benutzers.The path for the application data of a user.

Beispiele

Im folgenden Codebeispiel werden zwei Formulare angezeigt, und die Anwendung wird beendet, wenn beide Formulare geschlossen sind.The following code example displays two forms and exits the application when both forms are closed. Wenn die Anwendung gestartet und beendet wird, wird die Position der einzelnen Formulare gespeichert.When the application starts and exits, the position of each form is remembered. In diesem Beispiel wird die Verwendung der- UserAppDataPath Eigenschaft zum Speichern von Anwendungsdaten für den Benutzer veranschaulicht.This example demonstrates using the UserAppDataPath property to store application data for the user.

Die Klasse MyApplicationContext erbt von ApplicationContext und verfolgt, wenn jedes Formular geschlossen ist, und beendet den aktuellen Thread, wenn beide vorhanden sind.The class MyApplicationContext inherits from ApplicationContext and keeps track when each form is closed, and exits the current thread when they both are. Die-Klasse speichert die Positionen der einzelnen Formulare für den Benutzer.The class stores the positions of each form for the user. Die Formular Positionsdaten werden in einer Datei mit dem Namen gespeichert Appdata.txt , die an dem von festgelegten Speicherort erstellt wird UserAppDataPath .The form position data is stored in a file titled Appdata.txt that is created in the location determined by UserAppDataPath. Die- Main Methode ruft Application.Run(context) auf, um die Anwendung mit dem zu starten ApplicationContext .The Main method calls Application.Run(context) to start the application given the ApplicationContext.

Dieser Code ist ein Auszug aus dem Beispiel in der ApplicationContext Übersicht über die-Klasse.This code is an excerpt from the example shown in the ApplicationContext class overview. Zum Zweck der über sichtlichkeit wird Code nicht angezeigt.Some code is not shown for the purpose of brevity. ApplicationContextDas gesamte Codelisting finden Sie unter.See ApplicationContext for the whole code listing.

   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

Hinweise

Wenn kein Pfad vorhanden ist, wird ein Pfad im folgenden Format erstellt:If a path does not exist, one is created in the following format:

Basispfad\CompanyName\ProductName\ProductVersionBase Path\CompanyName\ProductName\ProductVersion

Die in diesem Pfad gespeicherten Daten sind Teil des Benutzerprofils, das für das Roaming aktiviert ist.Data stored in this path is part of user profile that is enabled for roaming. Ein Roamingbenutzer arbeitet auf mehr als einem Computer in einem Netzwerk.A roaming user works on more than one computer in a network. Das Benutzerprofil für einen Roamingbenutzer wird auf einem Server im Netzwerk aufbewahrt und auf ein System geladen, wenn sich der Benutzer anmeldet.The user profile for a roaming user is kept on a server on the network and is loaded onto a system when the user logs on. Damit ein Benutzerprofil für das Roaming infrage kommen kann, muss das Betriebssystem Roamingprofile unterstützen, und es muss aktiviert sein.For a user profile to be considered for roaming, the operating system must support roaming profiles and it must be enabled.

Ein typischer Basispfad lautet "c:\Dokumente und Einstellungen \ Benutzername\Anwendungsdaten".A typical base path is C:\Documents and Settings\username\Application Data. Dieser Pfad ist jedoch unterschiedlich, wenn die Windows Forms Anwendung mithilfe von bereitgestellt wird ClickOnceClickOnce .This path will be different, however, if the Windows Forms application is deployed by using ClickOnceClickOnce. ClickOnceClickOnce erstellt ein eigenes Anwendungsdatenverzeichnis, das von allen anderen Anwendungen isoliert ist.creates its own application data directory that is isolated from all other applications. Weitere Informationen finden Sie unter zugreifen auf lokale und Remote Daten in ClickOnce-Anwendungen.For more information, see Accessing Local and Remote Data in ClickOnce Applications.

Gilt für:

Weitere Informationen