Application.LocalUserAppDataPath Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient le chemin d’accès aux données d’application d’un utilisateur local non itinérant.
public:
static property System::String ^ LocalUserAppDataPath { System::String ^ get(); };
public static string LocalUserAppDataPath { get; }
static member LocalUserAppDataPath : string
Public Shared ReadOnly Property LocalUserAppDataPath As String
Valeur de propriété
Chemin d’accès aux données d’application d’un utilisateur local non itinérant.
Exemples
L’exemple de code suivant affiche deux formulaires et quitte l’application lorsque les deux formulaires sont fermés. Lorsque l’application démarre et se termine, la position de chaque formulaire est mémorisé. Bien que cet exemple illustre l’utilisation de la UserAppDataPath propriété pour stocker des données d’application pour l’utilisateur, le peut être utilisé à la LocalUserAppDataPath place.
La MyApplicationContext
classe hérite de ApplicationContext et effectue le suivi lorsque chaque formulaire est fermé, et quitte le thread actuel lorsqu’ils le sont tous deux. La classe stocke les positions de chaque formulaire pour l’utilisateur. Les données de position du formulaire sont stockées dans un fichier intitulé Appdata.txt
qui est créé à l’emplacement déterminé par UserAppDataPath. La Main
méthode appelle Application.Run(context)
pour démarrer l’application en fonction de .ApplicationContext
Ce code est un extrait de l’exemple présenté dans la vue d’ensemble de la ApplicationContext classe. Certains codes ne sont pas affichés à des fins de concision. Consultez ApplicationContext la liste complète du code.
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
Un utilisateur local est un utilisateur dont le profil utilisateur est stocké sur le système sur lequel l’utilisateur s’est connecté. S’il n’existe pas de chemin d’accès, un chemin d’accès est créé au format suivant :
Chemin d’accès de base\CompanyName\ProductName\ProductVersion
Un chemin de base classique est C:\Documents and Settings\username\Local Settings\Application Data. Ce chemin sera toutefois différent si l’application Windows Forms est déployée à l’aide de ClickOnce. ClickOnce crée son propre répertoire de données d’application isolé de toutes les autres applications. Pour plus d’informations, consultez Accès aux données locales et distantes dans les applications ClickOnce.