我目前正在开发一个示例 Windows 8 应用程序,它加载一个日志文件并对其进行处理,以便在 DevExpress XtraGrid 中显示。当我将所需的扩展名添加到文件类型筛选器时,代码会引发 UnauthorizedAccessException,即使我将文件扩展名添加到 appxmanifest :
private void OpenFile()
{
try
{
FileOpenPicker pickLog = new FileOpenPicker();
pickLog.CommitButtonText = "Logdatei öffnen";
pickLog.SuggestedStartLocation = PickerLocationId.ComputerFolder;
pickLog.ViewMode = PickerViewMode.List;
pickLog.FileTypeFilter.Add(".log"); //This is where the code jumps out
pickLog.FileTypeFilter.Add(".slg");
pickLog.PickSingleFileAsync().Completed += delegate
{
StorageFile logFile = pickLog.PickSingleFileAsync().GetResults();
Stream strLog = logFile.OpenStreamForReadAsync().Result;
vm.LoadCommand.Execute(strLog);
};
pickLog.PickSingleFileAsync();
}
catch (Exception ex) //Catches UnauthorizedAccessException
{
MessageDialog md = new MessageDialog(ex.Message, ex.GetType().ToString());
md.ShowAsync();
}
}
更糟糕的是,如果我注释掉 FileTypeFilter 行,代码会在我添加的匿名方法处跳出:
private void OpenFile()
{
try
{
FileOpenPicker pickLog = new FileOpenPicker();
pickLog.CommitButtonText = "Logdatei öffnen";
pickLog.SuggestedStartLocation = PickerLocationId.ComputerFolder;
pickLog.ViewMode = PickerViewMode.List;
//pickLog.FileTypeFilter.Add(".log");
//pickLog.FileTypeFilter.Add(".slg");
pickLog.PickSingleFileAsync().Completed += delegate //This is where the code jumps out
{
StorageFile logFile = pickLog.PickSingleFileAsync().GetResults();
Stream strLog = logFile.OpenStreamForReadAsync().Result;
vm.LoadCommand.Execute(strLog);
};
pickLog.PickSingleFileAsync();
}
catch (Exception ex) //Catches COMException
{
MessageDialog md = new MessageDialog(ex.Message, ex.GetType().ToString());
md.ShowAsync();
}
}
我连续几天进行了彻底的研究,但没有工作结果(包括 StackOverflow 在内的来源),这就是为什么我在这里提出这个问题。我感谢这里提供的任何帮助:)
更新 :
抛出 COMException 时,HRESULT 始终为 (0x80070005),但内部 HRESULT(显示在详细信息窗口中的 HRESULT)通常为 -21474xxxx,但是当我在 VS 中使用提升权限调试我的应用程序时,内部 HRESULT 为 -2147024891 .