Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Access library safely
private async Task<StorageLibrary> TryAccessLibraryAsync(KnownLibraryId library)
return await StorageLibrary.GetLibraryAsync(library);
catch (UnauthorizedAccessException)
//explain the issue
MessageDialog requestPermissionDialog =
new MessageDialog(
$"The app needs to access the {library}. " +
"Press OK to open system settings and give this app permission. " +
"If the app closes, reopen it afterwards. " +
"If you Cancel, the app will have limited functionality only.");
var okCommand = new UICommand("OK");
var cancelCommand = new UICommand("Cancel");
requestPermissionDialog.DefaultCommandIndex = 0;
requestPermissionDialog.CancelCommandIndex = 1;
var requestPermissionResult = await requestPermissionDialog.ShowAsync();
if (requestPermissionResult == cancelCommand)
//user chose to Cancel, app will not have permission
return null;
//open app settings to allow users to give us permission
await Launcher.LaunchUriAsync(new Uri("ms-settings:appsfeatures-app"));
//confirmation dialog to retry
var confirmationDialog = new MessageDialog(
$"Please give this app the {library} permission " +
"in the Settings app which has now opened.");
await confirmationDialog.ShowAsync();
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment