【发布时间】:2013-02-15 12:55:09
【问题描述】:
我将数据保存在隔离存储中,但是当我重新启动手机时,我无法从那里读取这些数据。隔离存储为空。为什么?
如果我不关手机一切正常
这是我的代码:
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
{
}
}
}
在隔离存储中创建文件的功能
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;
}
【问题讨论】:
-
你是用真机还是模拟器?
-
应用启动时会发生什么?是否调用了 OpenFile()?您应该为 OpenFile 使用的 FileMode 应该是 FileMode.OpenOrCreate,而不是 FileMode.Open。 FileMode.Open 将始终重新创建文件并覆盖您之前拥有的任何数据。
-
我在模拟器上测试过。我已经在手机上运行应用程序,一切都开始工作了。谢谢
标签: c# windows-phone-7