WP7将数据保存在独立存储中

时间:2013-02-15 12:55:09

标签: c# windows-phone-7

我将数据保存在独立存储中,但是当我重新启动手机时,我无法从那里读取数据。隔离存储是空的。为什么呢?

如果我不关闭手机所有工作确定

这是我的代码:

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

功能在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;
            }

1 个答案:

答案 0 :(得分:2)

如果您正在讨论在模拟器上运行此操作,则正常行为。默认情况下,模拟器不会保留隔离存储。

物理设备将始终将数据保留在存储中,除非显式重置,应用程序已卸载或用户通过应用程序提供的方法之一删除了内容。

相关问题