简体   繁体   中英

How to get a file and move it out of the isolated storage?

I want to take a file that stored already in the isolated storage, and copy it out, somewhere on the disk.

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

That doesn't work. Throws IsolatedStorageException and says "Operation not permitted"

I don't see anything in the docs, other than this , which just says that "Some operations aren't permitted", but doesn't say what, exactly. My guess is that it doesn't want you copying out of isolated storage to arbitrary locations on disk. The docs do state that the destination can't be a directory, but even if you fix that, you still get the same error.

As a workaround, you can open the file, read its contents, and write them to another file like so.

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM