简体   繁体   中英

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

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.

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