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