2010-09-03 68 views
0

我想将已存储在独立存储中的文件复制到磁盘上的某个位置。如何获取文件并将其移出隔离存储?

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

这是行不通的。抛出IsolatedStorageException并说“操作不允许”

+0

如果操作不被允许,你不能这样做。 – SLaks 2010-09-03 18:53:35

+1

在Silverlight中,还是在桌面.NET应用程序中? – dthorpe 2010-09-03 18:53:52

回答

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); 
     } 
    } 
} 
相关问题