我想获取一个已经存储在隔离存储中的文件,并将其复制到磁盘上的某个位置。
IsolatedStorageFile.CopyFile("storedFile.txt","c:\temp")
那是行不通的。抛出 IsolatedStorageException 并说“不允许操作”
我想获取一个已经存储在隔离存储中的文件,并将其复制到磁盘上的某个位置。
IsolatedStorageFile.CopyFile("storedFile.txt","c:\temp")
那是行不通的。抛出 IsolatedStorageException 并说“不允许操作”
除了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);
}
}
}