前提条件:Windows 8 Pro RTM + VS2012 Ultimate RTM
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.app启动时,在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;
错误: 用户代码未处理 UnauthorizedAccessException 访问被拒绝。(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))
I check the folder, the xml file is created when the error happen.
.4. 更多:当我重新打开应用程序时,什么也没有发生,它就像一个魅力。我不知道为什么,该应用程序在 Win8 RP 和 VS 2012 RC 中运行良好。我什么都没改变,但为什么它不能在带有 VC2012 RTM 的 Win8 RTM 中运行