Skip to content

Commit 4813388

Browse files
authored
Fixed an issue where the file access consent dialog would sometimes show even after permission was already granted (#1917)
1 parent 6fd7044 commit 4813388

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

Files/Filesystem/Drives.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ public DrivesManager()
4040
private async void EnumerateDrives()
4141
{
4242
_driveEnumInProgress = true;
43-
try
43+
if (await GetDrives(Drives))
4444
{
45-
await GetDrives(Drives);
46-
GetVirtualDrivesList(Drives);
47-
StartDeviceWatcher();
45+
if (!Drives.Any(d => d.Type != DriveType.Removable))
46+
{
47+
// Only show consent dialog if the exception is UnauthorizedAccessException
48+
// and the drives list is empty (except for Removable drives which don't require FileSystem access)
49+
ShowUserConsentOnInit = true;
50+
}
4851
}
49-
catch (AggregateException ex)
50-
{
51-
Logger.Warn(ex, ex.Message);
52-
ShowUserConsentOnInit = true;
53-
};
52+
GetVirtualDrivesList(Drives);
53+
StartDeviceWatcher();
5454
_driveEnumInProgress = false;
5555
}
5656

@@ -201,8 +201,11 @@ private void DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args)
201201
Debug.WriteLine("Devices updated");
202202
}
203203

204-
private async Task GetDrives(IList<DriveItem> list)
204+
private async Task<bool> GetDrives(IList<DriveItem> list)
205205
{
206+
// Flag set if any drive throws UnauthorizedAccessException
207+
bool unauthorizedAccessDetected = false;
208+
206209
var drives = DriveInfo.GetDrives().ToList();
207210

208211
var remDevices = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector());
@@ -235,7 +238,22 @@ private async Task GetDrives(IList<DriveItem> list)
235238
continue;
236239
}
237240

238-
var folder = Task.Run(async () => await StorageFolder.GetFolderFromPathAsync(drive.Name)).Result;
241+
StorageFolder folder = null;
242+
try
243+
{
244+
folder = await StorageFolder.GetFolderFromPathAsync(drive.Name);
245+
}
246+
catch (UnauthorizedAccessException ex)
247+
{
248+
unauthorizedAccessDetected = true;
249+
Logger.Warn($"{ex.GetType()}: Attemting to add the device, {drive.Name}, failed at the StorageFolder initialization step. This device will be ignored.");
250+
continue;
251+
}
252+
catch (ArgumentException ex)
253+
{
254+
Logger.Warn($"{ex.GetType()}: Attemting to add the device, {drive.Name}, failed at the StorageFolder initialization step. This device will be ignored.");
255+
continue;
256+
}
239257

240258
DriveType type = DriveType.Unknown;
241259

@@ -289,6 +307,8 @@ private async Task GetDrives(IList<DriveItem> list)
289307

290308
list.Add(driveItem);
291309
}
310+
311+
return unauthorizedAccessDetected;
292312
}
293313

294314
private void GetVirtualDrivesList(IList<DriveItem> list)

0 commit comments

Comments
 (0)