1

I'm trying to implement SkyDrive backup into my app. Everything is working fine except I have no idea how to easily save stream (with downloaded backup from SkyDrive) into Isolated Storarage.

I know that I could deserilaze the stream and than save it, but it would unnecessarily complicated.

So, here's my problem:

I have a Stream with the file from SkyDrive and I need to save it into IsolatedStorage as file 'Settings.XML'.

So basicly I need to Write the 'stream' into Settings.xml file in Isolated Storage

        static void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e, string saveToPath)
    {
        Stream stream = e.Result; //Need to write this into IS

        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(saveToPath, FileMode.Create))
                {
                    //How to save it?

                }
            }
        }...

Thanks!

Petrroll
  • 741
  • 7
  • 29

2 Answers2

2

Use the steam's CopyTo() method - http://msdn.microsoft.com/en-us/library/dd782932.aspx

using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(saveToPath, FileMode.Create))    
{    
  stream.CopyTo(fileStream);
} 
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
2

The quickest and simplest thung to do is to copy the bytes block by block from one stream to the next - see the accepted answer in How do I copy the contents of one stream to another?


However, if there is any chance that the network stream will be corrupt or contain invalid data, then it might be worth deserializing the stream and validating before you save it.

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • Ok, I'll copy the content of SkyDrive stream into local IsolatedStorage Stream. But how will I save it then (into file)? – Petrroll May 19 '12 at 15:41
  • That iso stream in your code sample is backed by a file :) that's what openFile gives you. – Stuart May 19 '12 at 15:45
  • `using` means that `Dispose()` will be called on the stream - which will mean `Close()` happens – Stuart May 19 '12 at 16:09