Application.LocalUserAppDataPath 属性

获取本地、非漫游用户的应用程序数据的路径。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Shared ReadOnly Property LocalUserAppDataPath As String
用法
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

属性值

本地、非漫游用户的应用程序数据的路径。

备注

本地用户是其用户配置文件存储在该用户所登录的系统中的用户。如果不存在路径,则会使用下列格式创建一个:

基路径\CompanyName\ProductName\ProductVersion

典型的基路径为 C:\Documents and Settings\username\Local Settings\Application Data。

示例

下面的代码示例显示两个窗体,并在两个窗体都关闭时退出应用程序。在应用程序启动和退出时,它会记住每个窗体的位置。虽然本示例演示了使用 UserAppDataPath 属性为用户存储应用程序数据,但也可以改用 LocalUserAppDataPath

MyApplicationContext 类从 ApplicationContext 继承,并在每个窗体关闭时保持跟踪,在两个窗体都关闭时退出当前线程。该类为用户存储每个窗体的位置。窗体位置数据存储在标题为 Appdata.txt 的文件中,该文件在 UserAppDataPath 确定的位置中创建。给定 ApplicationContext 的情况下,Main 方法调用 Application.Run(context) 启动应用程序。

这段代码摘录自 ApplicationContext 类概述中显示的示例。为了简洁,一些代码没有显示。有关全部代码的列表,请参见 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

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Application 类
Application 成员
System.Windows.Forms 命名空间