Application.UserAppDataPath Propriété

Définition

Obtient le chemin d’accès aux données d’application d’un utilisateur.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

Valeur de propriété

String

Chemin d’accès aux données d’application d’un utilisateur.The path for the application data of a user.

Exemples

L’exemple de code suivant affiche deux formulaires et quitte l’application lorsque les deux formulaires sont fermés.The following code example displays two forms and exits the application when both forms are closed. Lorsque l’application démarre et se termine, la position de chaque formulaire est mémorisée.When the application starts and exits, the position of each form is remembered. Cet exemple illustre l’utilisation de la propriété UserAppDataPath pour stocker des données d’application pour l’utilisateur.This example demonstrates using the UserAppDataPath property to store application data for the user.

La classe MyApplicationContext hérite de ApplicationContext et assure le suivi de la fermeture de chaque formulaire, puis quitte le thread actuel lorsqu’ils sont tous les deux fermés.The class MyApplicationContext inherits from ApplicationContext and keeps track when each form is closed, and exits the current thread when they both are. La classe stocke les positions de chaque formulaire pour l’utilisateur.The class stores the positions of each form for the user. Les données de position de formulaire sont stockées dans un fichier intitulé Appdata.txt qui est créé dans l’emplacement déterminé par UserAppDataPath.The form position data is stored in a file titled Appdata.txt that is created in the location determined by UserAppDataPath. La méthode Main appelle Application.Run(context) pour démarrer l’application en fonction de l' ApplicationContext.The Main method calls Application.Run(context) to start the application given the ApplicationContext.

Ce code est un extrait de l’exemple présenté dans la vue d’ensemble de la classe ApplicationContext.This code is an excerpt from the example shown in the ApplicationContext class overview. Du code n’est pas affiché à des fins de concision.Some code is not shown for the purpose of brevity. Consultez ApplicationContext pour obtenir la liste de code complète.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

Remarques

Si aucun chemin d’accès n’existe, un chemin d’accès est créé au format suivant :If a path does not exist, one is created in the following format:

Chemin de Base\CompanyName\ProductName\ProductVersionBase Path\CompanyName\ProductName\ProductVersion

Les données stockées dans ce chemin d’accès font partie du profil utilisateur qui est activé pour l’itinérance.Data stored in this path is part of user profile that is enabled for roaming. Un utilisateur itinérant travaille sur plusieurs ordinateurs d’un réseau.A roaming user works on more than one computer in a network. Le profil utilisateur d’un utilisateur itinérant est conservé sur un serveur sur le réseau et est chargé sur un système lorsque l’utilisateur ouvre une session.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. Pour qu’un profil utilisateur soit pris en compte pour l’itinérance, le système d’exploitation doit prendre en charge les profils itinérants et doit être activé.For a user profile to be considered for roaming, the operating system must support roaming profiles and it must be enabled.

Un chemin d’accès de base standard est C:\Documents and Settings\username\Application Data.A typical base path is C:\Documents and Settings\username\Application Data. Toutefois, ce chemin d’accès sera différent si l’application Windows Forms est déployée à l’aide de ClickOnceClickOnce.This path will be different, however, if the Windows Forms application is deployed by using ClickOnceClickOnce. ClickOnceClickOnce crée son propre répertoire de données d’application qui est isolé de toutes les autres applications.creates its own application data directory that is isolated from all other applications. Pour plus d’informations, consultez accès aux données locales et distantes dans les applications ClickOnce.For more information, see Accessing Local and Remote Data in ClickOnce Applications.

S’applique à

Voir aussi