UnauthorizedAccessException on reading a picked file

Heiko 1,261 Reputation points
2021-01-12T17:58:36.777+00:00

Hello,

With the FileOpenPicker the user opens a file, e.g. from the directory D:\temp. Then the path from StorageFile.Path is passed to read this file. Unfortunately, when I try to open the file, I get an UnauthorizedAccessException. If the file would be opened via StorageFile.OpenStreamForReadAsync(), there would be no exception and one could read the file.

Last year (2020) the file accesses still worked without errors. Now the following calls no longer work:

File.ReadAllText(file.Path);
ZipFile.OpenRead(file.Path);
pDWriteFactory->CreateFontFileReference(file->Path->Data(), NULL, &pFontFile); // In a C++/CX DLL

I can reproduce the problem with the following example:

private async void button_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker  picker = new FileOpenPicker();
    StorageFile file;
    string      msg = "?";

    picker.FileTypeFilter.Add("*");
    picker.ViewMode = PickerViewMode.List;
    picker.SuggestedStartLocation = PickerLocationId.ComputerFolder;

    file = await picker.PickSingleFileAsync();
    if (file == null)
        return;

    try
    {
        // This works.
        //using (Stream input = await file.OpenStreamForReadAsync())
        //using (StreamReader reader = new StreamReader(input))
        //{
        //  msg = "OK\r\n";

        //  string  text = reader.ReadToEnd();

        //  msg += text.Length > 50 ? text.Substring(0, 50) : text;
        //}

        // This throws an UnauthorizedAccessException for local files, but not for files on a mapped network drive.
        await Task.Run(() =>
            {
                try
                {
                    string text = File.ReadAllText(file.Path);

                    msg = "OK\r\n";
                    msg += text.Length > 50 ? text.Substring(0, 50) : text;
                }
                catch (Exception ex2)
                {
                    msg = ex2.GetType().Name + ": " + ex2.Message + "; 0x" + ex2.HResult.ToString("X8");
                }
            });
    }
    catch (Exception ex)
    {
        msg = ex.GetType().Name + ": " + ex.Message + "; 0x" + ex.HResult.ToString("X8");
    }
    await new MessageDialog(msg).ShowAsync();
}

On my PC, I have not changed the directories and files since last year. Last year I could still open local files without any problems. Now I can only access files that are in a network directory mapped to a drive letter.

What has changed in the runtime?

VS 2017 15.9.30
Windows 10.0.19042.685

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 33,366 Reputation points Microsoft Vendor
    2021-01-13T01:57:24.183+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The behavior you got is expected when you using File.ReadAllText() to access the file. UWP apps are running in the sandbox so it is isolated. There are limitations in UWP apps when you trying to access the file system. You could not access the file via Path using File Class. When you picked it using a picker, it does not mean you could access the file directly using File.ReadAllText(), you have to use Windows.Storage API to get the content of the file like what you do at the beginning .

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.