如何获取文件并将其移出隔离存储?

时间:2010-09-03 18:51:59

标签: c# .net .net-4.0 isolatedstorage

我想要一个已经存储在隔离存储中的文件,并将其复制到磁盘上的某个位置。

 IsolatedStorageFile.CopyFile("storedFile.txt","c:\temp") 

这不起作用。抛出IsolatedStorageException并说“不允许操作”

1 个答案:

答案 0 :(得分:0)

除了this之外,我没有在文档中看到任何内容,它只是说“某些操作是不允许的”,但并没有说明确切的内容。我的猜测是它不希望你将独立存储复制到磁盘上的任意位置。文档确实说明目标不能是目录,但即使你修复了它,你仍会得到同样的错误。

作为一种解决方法,您可以打开文件,读取其内容,然后将其写入另一个文件。

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForAssembly())
{
    //write sample file
    using (Stream fs = new IsolatedStorageFileStream("test.txt", FileMode.Create, store))
    {
        StreamWriter w = new StreamWriter(fs);
        w.WriteLine("test");
        w.Flush();
    }

    //the following line will crash...
    //store.CopyFile("test.txt", @"c:\test2.txt");

    //open the file backup, read its contents, write them back out to 
    //your new file.
    using (IsolatedStorageFileStream ifs = store.OpenFile("test.txt", FileMode.Open))
    {
        StreamReader reader = new StreamReader(ifs);
        string contents = reader.ReadToEnd();
        using (StreamWriter sw = new StreamWriter("nonisostorage.txt"))
        {
            sw.Write(contents);
        }
    }
}