我想要的
从 AppData.Local 中获取一个 xml 文件,并将其序列化为一个列表
我编码什么
错误部分:
List<myTask> AllTaskList = await objectStorageHelper.LoadAsync();
myTask 是一个简单的类:
public class myTask
{
public string myTitle { get; set; }
public string myDuetime { get; set; }
}
objectStorageHelper 是来自 CodePlex 的 HelpClass , LoadAsync 部分如下:
public async Task<T> LoadAsync()
{
try
{
StorageFile file = null;
StorageFolder folder = GetFolder(storageType);
file = await folder.GetFileAsync(FileName());
//file = await folder.CreateFileAsync("BetterTask.xml", CreationCollisionOption.OpenIfExists);
IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);
Stream inStream = Task.Run(() => readStream.AsStreamForRead()).Result;
return (T)serializer.Deserialize(inStream);
}
catch (FileNotFoundException)
{
//file not existing is perfectly valid so simply return the default
return default(T);
//Interesting thread here: How to detect if a file exists (http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/1eb71a80-c59c-4146-aeb6-fefd69f4b4bb)
//throw;
}
catch (Exception)
{
//Unable to load contents of file
throw;
}
}
错误是什么
An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code
Additional information: Access Denied。 (Exception from HRESULT:0x80070005 (E_ACCESSDENIED))
If there is a handler for this exception, the program may be safely continued.
--
为什么会这样?
我可以使用这个帮助类成功写入文件。
但是为什么我没有读取文件的权限?
如何解决?
最佳答案
改变
StorageFolder folder = GetFolder(storageType);
到
StorageFolder folder = ApplicationData.Current.LocalFolder
如果这可行,则问题出在您的文件夹权限上。
关于c# - 为什么我不能在 WinRT 中从 AppData 读取 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11063592/