使用独立存储时"无法找到文件"

Pet*_*jan 5 .net c#

我将东西保存在Isolated Storage文件中(使用IsolatedStorageFile类).它运行良好,我可以从GUI层调用DAL层中的保存和检索方法时检索保存的值.但是,当我尝试从同一项目中的另一个程序集中检索相同的设置时,它会给我一个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;
    }
Run Code Online (Sandbox Code Playgroud)

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

编辑:因为我需要几个程序集来访问文件,所以除了它是一个ClickOnce应用程序之外,它似乎不可能与隔离存储有关.

ice*_*ava 4

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

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

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