"找不到文件"使用独立存储时, [英] "Could not find file" when using Isolated Storage

查看:121
本文介绍了"找不到文件"使用独立存储时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存的东西,在href="http://msdn.microsoft.com/en-us/library/3ak841sy.aspx" rel="nofollow">独立存储文件的 DAL 层从我的GUI层。然而,当我尝试从在同一项目中的另一个组件检索相同的设置,它给了我一个FileNotFoundException异常。我该怎么办错了吗?这是一般的概念:

I save stuff in an Isolated Storage file (using class IsolatedStorageFile). It works well, and I can retrieve the saved values when calling the saving and retrieving methods in my DAL layer from my GUI layer. However, when I try to retrieve the same settings from another assembly in the same project, it gives me a FileNotFoundException. What do I do wrong? This is the general concept:

    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 *范围尝试。

I've tried using all the GetMachineStoreFor* scopes.

编辑:由于我需要一些程序集访问文件,它似乎可能并没有做独立存储,除非它是一个的的ClickOnce应用程序

Since I need several assemblies to access the files, it doesn't seem possible to do with isolated storage, unless it's a ClickOnce application.

推荐答案

在实例化IsolatedStorageFile,你有没有范围,它IsolatedStorageScope.Machine?

When you instantiated the IsolatedStorageFile, did you scope it to IsolatedStorageScope.Machine?

好了,现在你已经说明了你的code的风格,我已经回到重新测试的方法的行为,这里的解释是:

Ok now that you have illustrated your code style and I have gone back to retesting the behaviour of the methods, here is the explanation:

  • GetMachineStoreForAssembly() - 范围的机器和程序集标识。不同的组件在同一个应用程序将有自己的独立存储。
  • GetMachineStoreForDomain() - 在我看来是用词不当。作用于机器和对的程序集标识顶级域名标识的。应该有单独的AppDomain只是一个选项。
  • GetMachineStoreForApplication() - 这是您正在寻找的人。我测试了它,不同的组件可以拿起写在另一个组件中的值。唯一的缺点是,在应用程序标识的必须是可核查的。当本地运行,它不能被适当地确定,这将最终有例外无法确定呼叫者的应用程序标识。它可以通过点击一次部署应用程序进行验证。只有这样,才能应用此方法,实现共享的独立存储其应有的效果。
  • GetMachineStoreForAssembly() - scoped to the machine and the assembly identity. Different assemblies in the same application would have their own isolated storage.
  • GetMachineStoreForDomain() - a misnomer in my opinion. scoped to the machine and the domain identity on top of the assembly identity. There should have been an option for just AppDomain alone.
  • GetMachineStoreForApplication() - this is the one you are looking for. I have tested it and different assemblies can pick up the values written in another assembly. The only catch is, the application identity must be verifiable. When running locally, it cannot be properly determined and it will end up with exception "Unable to determine application identity of the caller". It can be verified by deploying the application via Click Once. Only then can this method apply and achieve its desired effect of shared isolated storage.

这篇关于"找不到文件"使用独立存储时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆