应用程序的用户配置文件加载 UserAppDataPath 属性

如果不存在路径,则会使用下列格式创建一个:

基路径\CompanyName\ProductName\ProductVersion

存储在此路径中的数据是漫游时启用的用户配置文件的一部分。 漫游用户在网络上的多台计算机上工作。 漫游用户的用户配置文件保留在网络中的某台服务器上,并在用户登录时加载到系统中。 对于要漫游的用户配置文件,操作系统必须支持漫游配置文件而且必须启用它。

典型的基路径为 C:\Documents and Settings\用户名\Application Data。 但是,如果使用 ClickOnce 来部署 Windows 窗体应用程序,此路径将会不同。 ClickOnce 创建其自己的应用程序数据目录,该目录与所有其他应用程序相隔离。 有关更多信息,请参见 在 ClickOnce 应用程序中访问本地数据和远程数据

 

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

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

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

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^ ) { } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值