ClickOnce和IsolatedStorage

Pat*_*ins 10 c# clickonce isolatedstorage .net-2.0 winforms

Winform应用程序在我们的Intranet中使用ClickOnce发布.我们在隔离存储中存储GUI的个人偏好.一切都很好:)

问题是,当我们有一个新版本的应用程序时,我们发布...所有偏好都丢失了!用户需要在每个版本上重复设置他们的首选项.

有没有办法冻结整个应用程序的隔离而不是版本?

cod*_*ion 18

您需要使用应用程序范围,而不是范围的隔离存储.这可以通过使用IsolatedStorageFileStream的重载构造函数之一来完成.

例:

using System.IO;
using System.IO.IsolatedStorage;
...

IsolatedStorageFile appScope = IsolatedStorageFile.GetUserStoreForApplication();    
using(IsolatedStorageFileStream fs = new IsolatedStorageFileStream("data.dat", FileMode.OpenOrCreate, appScope))
{
...
Run Code Online (Sandbox Code Playgroud)

但是,现在只有在通过ClickOnce启动应用程序时才会遇到此代码的问题,因为这是应用程序作用域隔离存储的唯一可用时间.如果不通过ClickOnce启动(例如通过Visual Studio),GetUserStoreForApplication()将抛出异常.

解决问题的方法是在尝试使用应用程序作用域隔离存储之前确保AppDomain.CurrentDomain.ActivationContext不为null.

  • 或者您可以使用*System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed*来了解应用程序是否使用ClickOnce部署,您可以使用GetUserStoreForApplication() (3认同)