C# WP7设备中隔离存储的奇怪问题
我的Windows Phone 7应用程序中有一个奇怪的问题。我需要在我的应用程序中读/写一些xml文件,我正在使用C# WP7设备中隔离存储的奇怪问题,c#,windows-phone-7,xml-serialization,isolatedstorage,onedrive,C#,Windows Phone 7,Xml Serialization,Isolatedstorage,Onedrive,我的Windows Phone 7应用程序中有一个奇怪的问题。我需要在我的应用程序中读/写一些xml文件,我正在使用IsolatedStorage收集数据。我的应用程序从SkyDrive发送/获取数据,这就是我使用它的原因 好的,下面是生成异常的函数: private void CreateFileIntoIsolatedStorage(List<Record> list) { isf = IsolatedStorageFile.GetUserStoreFor
IsolatedStorage
收集数据。我的应用程序从SkyDrive发送/获取数据,这就是我使用它的原因
好的,下面是生成异常的函数:
private void CreateFileIntoIsolatedStorage(List<Record> list)
{
isf = IsolatedStorageFile.GetUserStoreForApplication();
if(list.Count == 0)
list = new List<Record>() { new Record { Date = DateTime.Today, Value = 0 }};
if (isf.FileExists(fileName))
{
isf.DeleteFile(fileName);
}
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(fileName, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Record>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, list);
}
}
}
}
private void CreateFileIntoIsolatedStorage(列表)
{
isf=IsolatedStorageFile.GetUserStoreForApplication();
如果(list.Count==0)
list=newlist(){new Record{Date=DateTime.Today,Value=0};
如果(isf.FileExists(fileName))
{
删除文件(文件名);
}
XmlWriterSettings XmlWriterSettings=新的XmlWriterSettings();
xmlWriterSettings.Indent=true;
使用(IsolatedStorage File myIsolatedStorage=IsolatedStorage File.GetUserStoreForApplication())
{
使用(IsolatedStorageFileStream=myIsolatedStorage.OpenFile(文件名,FileMode.Create))
{
XmlSerializer serializer=新的XmlSerializer(typeof(List));
使用(XmlWriter=XmlWriter.Create(流,xmlWriterSettings))
{
serializer.Serialize(xmlWriter,list);
}
}
}
}
问题:
当我再次运行此函数时,问题就开始了。然后isf.DeleteFile(文件名)代码>抛出。并创建流
崩溃的应用程序
这很奇怪,因为每次我在我的设备上运行它时,它都会发生,很少在我使用调试器时发生
所以我的问题是如何解决这个问题,或者有没有更好的方法
任何帮助都将不胜感激。可能是因为在您的方法开始时,您有:
isf = IsolatedStorageFile.GetUserStoreForApplication();
你永远不会处理掉它。然后,稍后,您可以使用
在中再次获得它。但那个人已经决定了。然后,下次调用CreateFileInoIsolatedStorage
时,您会再次得到它,而不进行处理
也许这就是你想要的:
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if(list.Count == 0)
list = new List<Record>() { new Record { Date = DateTime.Today, Value = 0 }};
if (isf.FileExists(fileName))
{
isf.DeleteFile(fileName);
}
}
谢谢你,吉姆!我使用你的方法组合,它的作品完美!
int retryCount = 0;
while (retryCount < MaxRetryCount && isf.FileExists(fileName))
{
try
{
isf.DeleteFile(fileName);
}
catch (IsolatedStorageException)
{
++retryCount;
// maybe notify user and delay briefly
// or forget about the retry and log an error. Let user try it again.
}
}