Preconditions: Windows 8 Pro RTM + VS2012 Ultimate RTM为什么这段代码会在win8 rtm中引发异常,但不会在win8 rp中引发异常?
1.以下代码位于App.xaml.cs.中。 如果用户第一次打开此应用程序,该应用程序将创建一个xml。
private async void CheckXML()
{
try
{
StorageFile file1 = await ApplicationData.Current.LocalFolder.GetFileAsync("BetterTask.xml");
}
catch (FileNotFoundException)
{
CreateXML();
}
}
private async void CreateXML()
{
StorageFile file2 = await ApplicationData.Current.LocalFolder.CreateFileAsync("BetterTask.xml", CreationCollisionOption.ReplaceExisting);
using (var writeStream1 = await file2.OpenStreamForWriteAsync() as Stream)
{
XDocument doc1 = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("tasks",
new XElement("task",
new XElement("title", "Wish you enjoy this App :)"),
new XElement("due", DateTime.Now.Date.ToString("yyyy/MM/dd")))));
doc1.Save(writeStream1);
await writeStream1.FlushAsync();
}
}
2,当应用程序启动时,在MainPage.xaml中的事件的OnNavigatedTo,它将读取XML检索数据。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ReadTasks("all");
ShowTime();
}
private async void ReadTasks(string action)
{
StorageFile file1 = await ApplicationData.Current.LocalFolder.GetFileAsync(xmlfilename);
var ReadStream1 = await file1.OpenStreamForReadAsync() as Stream;
XDocument doc1 = new XDocument();
doc1 = XDocument.Load(ReadStream1);
ReadStream1.Dispose();
.......
}
.3。 会发生什么:第一次启动应用程序时,下面的代码抛出一个错误:
var ReadStream1 = await file1.OpenStreamForReadAsync() as Stream;
Error: UnauthorizedAccessException was unhandled by user code Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
I check the folder, the xml file is created when the error happen.
0.4。 更多内容:当我重新打开应用程序时,没有任何反应,它像一个魅力。我不知道为什么,该应用程序在Win8 RP和VS 2012 RC中运行良好。我改变什么,但为什么不能在Win8中RTM与VC2012运行RTM
它看起来像你对我试图打开文件时仍然被在不同的线程生成它。 – yms