Freigeben über


Application.LocalUserAppDataPath-Eigenschaft

Ruft den Pfad für die Anwendungsdaten eines lokalen Benutzers ab, der kein Roaming verwendet.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Shared ReadOnly Property LocalUserAppDataPath As String
'Usage
Dim value As String

value = Application.LocalUserAppDataPath
public static string LocalUserAppDataPath { get; }
public:
static property String^ LocalUserAppDataPath {
    String^ get ();
}
/** @property */
public static String get_LocalUserAppDataPath ()
public static function get LocalUserAppDataPath () : String

Eigenschaftenwert

Der Pfad für die Anwendungsdaten eines lokalen Benutzers, der kein Roaming verwendet.

Hinweise

Ein lokaler Benutzer ist ein Benutzer, dessen Benutzerprofil auf dem System gespeichert ist, an dem er sich angemeldet hat. Wenn ein Pfad nicht vorhanden ist, wird dieser entsprechend dem folgenden Format erstellt:

Basispfad\ CompanyName \ ProductName \ ProductVersion

Ein typischer Basispfad ist C:\Dokumente und Einstellungen\username\Lokale Einstellungen\Anwendungsdaten.

Beispiel

Im folgenden Codebeispiel werden zwei Formulare angezeigt, und die Anwendung wird beendet, wenn beide Formulare geschlossen werden. Beim Starten und Beenden der Anwendung wird die Position jedes Formulars gespeichert. In diesem Beispiel wird die Verwendung der UserAppDataPath-Eigenschaft zum Speichern von Anwendungsdaten für den Benutzer veranschaulicht. Stattdessen kann auch LocalUserAppDataPath verwendet werden.

Die MyApplicationContext-Klasse erbt von ApplicationContext und verfolgt das Schließen jedes Formulars. Außerdem wird der aktuelle Thread beendet, wenn beide geschlossen wurden. Die Klasse speichert die Positionen jedes Formulars für den Benutzer. Die Formularpositionsdaten werden in der Datei Appdata.txt gespeichert, die an dem von UserAppDataPath bestimmten Speicherort erstellt wird. Die Main-Methode ruft Application.Run(context) auf, um die Anwendung zu starten, wenn ApplicationContext angegeben ist.

Dieser Code ist ein Auszug aus dem Beispiel in der Übersicht zur ApplicationContext-Klasse. Teile des Codes wurden zur besseren Übersicht ausgelassen. Den vollständigen Code finden Sie unter ApplicationContext.

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
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 {}
}
   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.add_ApplicationExit(new EventHandler(
        this.OnApplicationExit));
    try {
        // Create a file that the application will store 
        // user specific data in.
        userData = new FileStream(Application.get_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.add_Closed(new EventHandler(OnFormClosed));
    form1.add_Closing(new CancelEventHandler(OnFormClosing));
    formCount++;
    form2 = new AppForm2();
    form2.add_Closed(new EventHandler(OnFormClosed));
    form2.add_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.set_StartPosition(FormStartPosition.Manual);
        form2.set_StartPosition(FormStartPosition.Manual);
        form1.set_Bounds(form1Position);
        form2.set_Bounds(form2Position);
    }

    // Show both forms.
    form1.Show();
    form2.Show();
} //MyApplicationContext

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 (System.Exception exp) {
    }
} //OnApplicationExit

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Application-Klasse
Application-Member
System.Windows.Forms-Namespace