Application.UserAppDataPath Application.UserAppDataPath Application.UserAppDataPath Application.UserAppDataPath Property

定义

获取用户的应用程序数据的路径。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

属性值

用户的应用程序数据的路径。The path for the application data of a user.

示例

下面的代码示例显示两个窗体并在两个窗体关闭时退出应用程序。The following code example displays two forms and exits the application when both forms are closed. 当应用程序启动和退出时,将记住每个窗体的位置。When the application starts and exits, the position of each form is remembered. 此示例演示如何使用 UserAppDataPath 属性来存储用户的应用程序数据。This example demonstrates using the UserAppDataPath property to store application data for the user.

类 @no__t 从 @no__t 继承,并在每个窗体关闭时保持跟踪,并在两个窗体关闭时保持跟踪,并在两个窗体都关闭时退出当前线程。The class MyApplicationContext inherits from ApplicationContext and keeps track when each form is closed, and exits the current thread when they both are. 类存储用户的每个窗体的位置。The class stores the positions of each form for the user. 表单位置数据存储在名为 Appdata.txt 的文件中,该文件在 UserAppDataPath 确定的位置中创建。The form position data is stored in a file titled Appdata.txt that is created in the location determined by UserAppDataPath. @No__t-0 方法调用 Application.Run(context),以便在给定 @no__t 2 的情况中启动应用程序。The Main method calls Application.Run(context) to start the application given the ApplicationContext.

此代码摘自 ApplicationContext 类概述中所示的示例。This code is an excerpt from the example shown in the ApplicationContext class overview. 为了简洁起见,不显示某些代码。Some code is not shown for the purpose of brevity. 有关完整代码列表,请参阅 ApplicationContextSee 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

注解

如果路径不存在,则将使用以下格式创建一个路径:If a path does not exist, one is created in the following format:

基路径\ @ no__t-2 @ no__t @ no__t @ @ no__t-5 @ no__t-6Base Path\CompanyName\ProductName\ProductVersion

存储在此路径中的数据是为漫游启用的用户配置文件的一部分。Data stored in this path is part of user profile that is enabled for roaming. 漫游用户在网络中的多台计算机上工作。A roaming user works on more than one computer in a network. 漫游用户的用户配置文件保存在网络上的服务器上,并在用户登录时加载到系统上。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. 对于要被视为漫游的用户配置文件,操作系统必须支持漫游配置文件,并且必须启用该配置文件。For a user profile to be considered for roaming, the operating system must support roaming profiles and it must be enabled.

典型的基路径为 "C:\documents and" 和 "Settings @ no__t-0username\Application Data"。A typical base path is C:\Documents and Settings\username\Application Data. 但如果使用 ClickOnceClickOnce 部署 Windows 窗体应用程序,则此路径将有所不同。This path will be different, however, if the Windows Forms application is deployed by using ClickOnceClickOnce. ClickOnceClickOnce 会创建自己的应用程序数据目录,该目录与所有其他应用程序隔离。creates its own application data directory that is isolated from all other applications. 有关详细信息,请参阅在 ClickOnce 应用程序中访问本地数据和远程数据For more information, see Accessing Local and Remote Data in ClickOnce Applications.

适用于

另请参阅