c# - 如何使用IsolatedStorageFile.GetUserStoreForApplication()

标签 c# isolatedstorage

我有一个 clickonce 应用程序,它在生产中运行良好,方法如下:

IsolatedStorageFile.GetUserStoreForApplication()

执行成功。 当我尝试调试我的应用程序时,由于“无法确定调用者的应用程序身份..”,它因isolatedStorageException而崩溃。 as described here

All assemblies associated with an application use the same isolated store when using this method. This method can be used only when the application identity can be determined - for example, when the application is published through ClickOnce deployment or is a Silverlight-based application. If you attempt to use this method outside a ClickOnce or Silverlight-based application, you will receive an IsolatedStorageException exception, because the application identity of the caller cannot be determined.

我的问题是如何使用IsolatedStorageFile.GetUserStoreForApplication()并毫无异常(exception)地调试应用程序?

  • 可能会进行一些检查?
  • 或使用自定义应用程序标识?
  • 或使用IsolatedStorageFile.GetEnumerator 获取可用的存储?

最佳答案

首先检查激活上下文是否为空,

public IsolatedStorageFile getIsolatedStorage() {
    return AppDomain.CurrentDomain.ActivationContext == null
        ? IsolatedStorageFile.GetUserStoreForAssembly()
        : IsolatedStorageFile.GetUserStoreForApplication();
}

这表明该域没有激活上下文,这意味着无法确定调用者的应用程序身份。

我还看到了另一个实现

引用ClickOnce and IsolatedStorage

他们在其中检查System.Deployment.Application.ApplicationDeployment.IsNetwor‌​kDeployed以确定部署后应用程序当前是否被单击

public IsolatedStorageFile getIsolatedStorage() {
    return System.Deployment.Application.ApplicationDeployment.IsNetwor‌​kDeployed
        ? IsolatedStorageFile.GetUserStoreForApplication()
        : IsolatedStorageFile.GetUserStoreForAssembly();
}

理想情况下,我还建议将 IsolatedStorage 封装在抽象后面,以便单元测试也可以单独完成,而不会产生链式 react 。

关于c# - 如何使用IsolatedStorageFile.GetUserStoreForApplication(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910720/

相关文章:

c# - 使用 SqlCommand.ExecuteNonQuery,如何获得超过 int 限制的受影响行数?

windows-8 - 为什么我无法访问 System.IO 命名空间中的isolatedStorage?

c# - 简单的隔离存储问题

c# - 在仍在下载的同时播放媒体文件(Windows Phone)

c# - 即使应用程序关闭,如何在 Windows Phone 的 IsolatedStorageSettings 中保留数据?

C# - 将对象保存到 JSON 文件

c# - Rabbitmq,在发布者之前重启消费者

c# - emguCV-快速计算二进制图像中带孔的对象的面积

c# - 对象引用未设置为对象的实例。(C# Web 应用程序)

c# - 无法在 XNA 中隐式转换类型