c# - 使用隔离存储时为 "Could not find file"

标签 c# .net

我将东西保存在 Isolated Storage 中文件(使用 IsolatedStorageFile 类)。它运行良好,我可以在我的 DAL 中调用保存和检索方法时检索保存的值。来 self 的 GUI 层的层。但是,当我尝试从同一项目中的另一个程序集检索相同的设置时,它会给我一个 FileNotFoundException。我做错了什么?这是一般概念:

    public void Save(int number)
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetMachineStoreForAssembly();
        IsolatedStorageFileStream fileStream =
            new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, storage);

        StreamWriter writer = new StreamWriter(fileStream);
        writer.WriteLine(number);
        writer.Close();
    }

    public int Retrieve()
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetMachineStoreForAssembly();
        IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filename, FileMode.Open, storage);

        StreamReader reader = new StreamReader(fileStream);

        int number;

        try
        {
            string line = reader.ReadLine();
            number = int.Parse(line);
        }
        finally
        {
            reader.Close();
        }

        return number;
    }

我已尝试使用所有 GetMachineStoreFor* 范围。

编辑:因为我需要多个程序集来访问文件,所以似乎不可能使用独立存储,除非它是 ClickOnce。申请。

最佳答案

当您实例化 IsolatedStorageFile 时,您是否将其范围限定为 IsolatedStorageScope.Machine?

好的,现在您已经说明了您的代码风格,我已经回去重新测试这些方法的行为,这里是解释:

  • GetMachineStoreForAssembly() - 范围限定为机器和程序集标识。同一应用程序中的不同程序集将有自己的隔离存储。
  • GetMachineStoreForDomain() - 我认为用词不当。范围为机器和域标识程序集标识之上。应该有一个单独的 AppDomain 选项。
  • GetMachineStoreForApplication() - 这就是您要找的那个。我已经对其进行了测试,不同的程序集可以获取另一个程序集中写入的值。唯一的问题是,应用程序身份 必须是可验证的。在本地运行时,无法正确确定,最终会出现异常“无法确定调用者的应用程序身份”。可以通过单击一次部署应用程序来验证它。只有这样,该方法才能应用并达到其共享隔离存储的预期效果。

关于c# - 使用隔离存储时为 "Could not find file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72626/

相关文章:

c# - 在 C# 中将 SID 转换为用户名

c# - 当用户单击该行的单元格时,如何选择完整的 dataGridView 行?

c# - 为什么我们需要反射?

c# - 在泛型类型定义中使用 'params' 的方法

c# - 在 Windows Phone 7 中使用 json webservice

c# - 如何在 C# 中使用 .net webbrowser 控件同时使用不同的帐户登录

c# - 何时删除 C# 中的定时器事件处理程序?

c# - 如何关闭计算机?

c# - 如果将摘要添加为引用,则摘要不会显示在程序集中

c# - 自定义 ConfigurationSection 默认 bool 值