Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;找不到文件";使用隔离存储时_C#_.net - Fatal编程技术网

C# &引用;找不到文件";使用隔离存储时

C# &引用;找不到文件";使用隔离存储时,c#,.net,C#,.net,我将内容保存在一个文件中(使用类IsolatedStorageFile)。它工作得很好,当从GUI层调用我的层中的saving和retrieval方法时,我可以检索保存的值。但是,当我尝试从同一项目中的另一个程序集检索相同的设置时,它会给我一个FileNotFoundException。我做错了什么?这是一般概念: public void Save(int number) { IsolatedStorageFile storage = IsolatedStorag

我将内容保存在一个文件中(使用类IsolatedStorageFile)。它工作得很好,当从GUI层调用我的层中的saving和retrieval方法时,我可以检索保存的值。但是,当我尝试从同一项目中的另一个程序集检索相同的设置时,它会给我一个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*范围


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

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

好了,现在您已经说明了您的代码风格,我又回到了重新测试方法行为的阶段,下面是解释:

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

在保存时,调用GetMachineStoreForDomain,但在检索时,调用GetMachineStoreForeFor

GetMachineStoreFromsembly的作用域是代码在其中执行的程序集,而GetMachineStoreFromain的作用域是当前运行的AppDomain和代码在其中执行的程序集。只要将这些调用更改为GetMachineStoreReplication,它就会工作


IsolatedStorageFile的文档可在

中找到。对不起,这是一个打字错误。我对save和retrieve方法使用了相同的作用域。所以文档对我没有帮助。正如我所说的,我可以成功地从GUI保存和检索,但另一个程序集无法检索。尝试了GetMachineStoreReplication(),但得到了未处理的IsolatedStorageException:无法确定调用方的应用程序标识是否可以将其更改为GetMachineStoreReplasembly?