Application.LocalUserAppDataPath Propiedad
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene la ruta de acceso de los datos de aplicación de un usuario local no móvil.
public:
static property System::String ^ LocalUserAppDataPath { System::String ^ get(); };
public static string LocalUserAppDataPath { get; }
member this.LocalUserAppDataPath : string
Public Shared ReadOnly Property LocalUserAppDataPath As String
Valor de propiedad
Ruta de acceso de los datos de aplicación de un usuario local no móvil.
Ejemplos
En el ejemplo de código siguiente se muestran dos formularios y se cierra la aplicación cuando ambos formularios están cerrados. Cuando se inicia y sale la aplicación, se recuerda la posición de cada formulario. Aunque en este ejemplo se muestra cómo usar la UserAppDataPath propiedad para almacenar los datos de la aplicación para el usuario, LocalUserAppDataPath se puede usar en su lugar.
La MyApplicationContext
clase hereda de ApplicationContext y realiza un seguimiento cuando se cierra cada formulario y sale del subproceso actual cuando ambos están. La clase almacena las posiciones de cada formulario para el usuario. Los datos de posición del formulario se almacenan en un archivo titulado Appdata.txt
que se crea en la ubicación determinada por UserAppDataPath. El Main
método llama Application.Run(context)
a para iniciar la aplicación según .ApplicationContext
Este código es un extracto del ejemplo que se muestra en la información general de la ApplicationContext clase. Algunos códigos no se muestran para la brevedad. Consulte ApplicationContext para obtener toda la lista de código.
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
Comentarios
Un usuario local es uno cuyo perfil de usuario se almacena en el sistema en el que el usuario ha iniciado sesión. Si no existe una ruta de acceso, se crea una en el formato siguiente:
Ruta de acceso base\CompanyName\ProductName\ProductVersion
Una ruta de acceso base típica es C:\Documents and Configuración\ username\Local Configuración\Application Data. Sin embargo, esta ruta de acceso será diferente si la aplicación de Windows Forms se implementa mediante ClickOnce. ClickOnce crea su propio directorio de datos de aplicación que está aislado de todas las demás aplicaciones. Para obtener más información, vea Obtener acceso a datos locales y remotos en ClickOnce Aplicaciones.