Nadezhda
Nadezhda

Reputation: 93

WP7 Saving data in isolated storage

I save data in isolated storage, but when I restart my Phone I can not read this data from there. Isolated storage is empty. Why?

If I don't switch off phone all work Ok

This is my code:

 using (Stream file = IsolatedStorageHelper.OpenFile(USER_ACCOUNT_FILE, fileMode.Create))
        {
            if (null != file)
            {
                try
                {
                    XDocument xml = new XDocument();
                    XElement root = new XElement("userAccount");

                    root.Add(new XAttribute("FirstName", this._firstName));
                    root.Add(new XAttribute("LastName", this._lastName));
                    root.Add(new XAttribute("ID", this._id));
                    root.Add(new XAttribute("Sex", this._sex));

                    xml.Add(root);

                    // save xml data
                    xml.Save(file);
                }
                catch
                {
                }
            }
        }

Function what create file in Issolated Storage

static public IsolatedStorageFileStream OpenFile(string aFilename, FileMode mode)
            {
                IsolatedStorageFileStream res = null;

                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {

                    try
                    {
                        res = new IsolatedStorageFileStream(aFilename, mode, FileAccess.ReadWrite, isoStore);
                    }
                    catch (Exception exc)
                    {
                        if ((null != (exc as IsolatedStorageException)) &&
                            (FileMode.Open != mode) &&
                            (true == createPathOnIsolatedStorage(isoStore,aFilename)) )
                        {
                            try
                            {
                                res = new IsolatedStorageFileStream(aFilename, mode, isoStore);
                            }
                            catch
                            {
                            }
                        }
                    }
                }

                return res;
            }

Upvotes: 0

Views: 499

Answers (1)

Den
Den

Reputation: 16826

If you are talking about running this on the emulator, this is normal behavior. The emulator does not preserve isolated storage by default.

A physical device will always keep the data in the storage unless it is explicitly reset, the application was uninstalled or the content was deleted by the user through one of the means provided by your application.

Upvotes: 2

Related Questions