简体   繁体   English

FileOpenPicker引发UnauthorizedAccessException

[英]FileOpenPicker throws UnauthorizedAccessException

i am currently developing a sample Windows 8 App which loads a Logfile and processes it for being shown in a DevExpress XtraGrid. 我目前正在开发一个示例Windows 8 App,该应用程序将加载一个日志文件并对其进行处理,以便在DevExpress XtraGrid中显示。 When i add the required extensions to the Filetype Filter, the code throws an UnauthorizedAccessException, even though i added the File extensions to the appxmanifest : 当我将所需的扩展名添加到文件类型过滤器时,即使我将文件扩展名添加到appxmanifest,代码也会引发UnauthorizedAccessException:

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();
        }
    }

Worse Thing is, that if i comment out the FileTypeFilter lines, the code jumps out at the anonymous method i added down there : 更糟糕的是,如果我注释掉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();
        }
    }

I did thorough researches for days on end without working results (sources including StackOverflow) which is, why i pose the question right here. 我连续几天进行了彻底的研究,却没有任何有效的结果(包括StackOverflow在内的资源),这就是为什么我在这里提出这个问题。 I appreciate any help given here :) 感谢您在此提供的帮助:)

UPDATE : 更新:

When COMException has been thrown, the HRESULT is always (0x80070005), but the inner HRESULT (the HRESULT displayed in the Details window) was normally -21474xxxx, but when i debug my app in VS with elevated right, the inner HRESULT is -2147024891. 引发COMException时,HRESULT始终为(0x80070005),但内部的HRESULT(在“详细信息”窗口中显示的HRESULT)通常为-21474xxxx,但是当我在VS中使用提升的权限调试我的应用程序时,内部的HRESULT为-2147024891 。

You don't seem to await the PickSingleFileAsync call. 您似乎并没有await PickSingleFileAsync调用。

You should be doing something like this : 您应该做这样的事情:

StorageFile file = await picker.PickSingleFileAsync(); 

After you have the StorageFile from the Pick operation, you can perform whatever operations you have to, against it. 从Pick操作中获取StorageFile之后,可以对其执行所需的任何操作。


You have to stop execution until a selection from the picker is returned. 您必须停止执行,直到返回选择器的选择。 Basically, this is handled for you with the line above. 基本上,这是通过上面的行为您处理的。

Moreover, I see that the MessageDialog 's ShowAsync is also an async call which is not awaited. 此外,我看到MessageDialogShowAsync也是一个未等待的异步调用。 The usage should be : 用法应为:

var messageDialog = new MessageDialog(...);
await messageDialog.ShowAsync();

or shorter : 或更短:

await new MessageDialog('','').ShowAsync();

Microsoft enforced this guideline of using the Async suffix to any method which is declared as async in order to be more obvious on how to use it. Microsoft强制对任何被声明为异步的方法都使用Async后缀,以便更清楚地了解如何使用它。 I suppose you should use it as well. 我想您也应该使用它。

As a good practice, if you're launching an async call, you will have to await it at some point, otherwise you might get unpredictable results which most of the time lead to application crash. 作为一种好的做法,如果要启动异步调用,则必须在某个时候等待它,否则您可能会得到无法预测的结果,在大多数情况下会导致应用程序崩溃。


Also, when you would like to show two message dialogs at the same type, you would run as well into this type of exception. 另外,当您想以相同的类型显示两个消息对话框时,也会遇到这种类型的异常。 You can have only one message dialog on the screen a time, and while the first is already being displayed, the second will attempt an operation which will throw the UnauthorizedAccessException . 一次只能在屏幕上显示一个消息对话框,而第一个消息对话框已经显示,第二个对话框将尝试执行操作,该操作将引发UnauthorizedAccessException


Edit 编辑

Here's how you should change your code : 这是更改代码的方法:

private async Task 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");

        StorageFile logFile = await pickLog.PickSingleFileAsync();

        //operations on logFile are safe to be done here (open stream, loadCommand etc)
    }
    catch (Exception ex) //Catches UnauthorizedAccessException
    {
        MessageDialog md = new MessageDialog(ex.Message, ex.GetType().ToString());
        md.ShowAsync();
    }
}

You don't need to add an event handler for the Completed event of the Picker. 您无需为Picker的Completed事件添加事件处理程序。 It is just enough to run your code as it is on the logFile after the PickSingleFileAsync call completed. PickSingleFileAsync调用完成之后, PickSingleFileAsynclogFile上运行代码即可。 I'm not able to provide a full working code because I'm not aware of your logic. 我无法提供完整的工作代码,因为我不知道您的逻辑。 But in any case, make sure you also await the OpenStreamForReadAsync call ( MSDN documentation ). 但无论如何,请确保您还await OpenStreamForReadAsync调用( MSDN文档 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileOpenPicker PickSingleFileAsync抛出UnauthorizedAccessException - FileOpenPicker PickSingleFileAsync throws UnauthorizedAccessException 使用FileOpenPicker时出现UnauthorizedAccessException - UnauthorizedAccessException when using FileOpenPicker NamedPipeClientStream 在 Connect 上抛出 UnauthorizedAccessException - NamedPipeClientStream throws UnauthorizedAccessException on Connect 谷歌 API .NET 抛出 UnauthorizedAccessException - Google API .NET Throws UnauthorizedAccessException ContactManager.RequestStoreAsync()抛出System.UnauthorizedAccessException - ContactManager.RequestStoreAsync() throws System.UnauthorizedAccessException GetAccessControl抛出UnauthorizedAccessException访问SystemData目录 - GetAccessControl throws UnauthorizedAccessException accessing SystemData directory MemoryMappedFile.CreateFromFile始终抛出UnauthorizedAccessException - MemoryMappedFile.CreateFromFile always throws UnauthorizedAccessException 下载文件引发 UnauthorizedAccessException net core 2 - Downloading a file throws UnauthorizedAccessException net core 2 NamedPipeClientStream 抛出 UnauthorizedAccessException:对路径的访问被拒绝 - NamedPipeClientStream throws UnauthorizedAccessException: Access to the path is denied C# 注册表 SetValue 抛出 UnauthorizedAccessException - C# Registry SetValue throws UnauthorizedAccessException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM