Database file restore error from skydrive because it is already in use by current application in windows phone

StackOverflow https://stackoverflow.com/questions/16805202

Question

private void OnRestoreClicked(object sender, RoutedEventArgs e) {

        string id = string.Empty;
        client.GetCompleted += (obj, args) =>
        {
            List<object> items = args.Result["data"] as List<object>;
            foreach (object item in items)
            {
                Dictionary<string, object> file = item as Dictionary<string, object>;
                if (file["name"].ToString() == "Expensemanager.bak")
                {
                    id = file["id"].ToString();
                }
            }

            client.DownloadCompleted += (o, a) =>
            {
                Stream stream = a.Result;

                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                {    
                         var fileToSave = new IsolatedStorageFileStream("expanseManager.sdf", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, storage);

                        stream.CopyTo(fileToSave);
                        stream.Flush();
                        stream.Close();

                }
            };

            client.DownloadAsync(string.Format("{0}/content", id));
        };

        client.GetAsync("me/skydrive/files");
    }

Error in this below line ...Operation not permitted on isolated storage ..

var fileToSave = new IsolatedStorageFileStream("expanseManager.sdf", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, storage);

Was it helpful?

Solution

You need to make sure your database is closed before you can overwrite the sdf file. If you are keeping a global DataContext reference then you need to dispose it. Other than that, just make sure to wrap your db interactions within a using block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top