C# 如何检查用户是否已授予应用对图片文件夹的权限?
我的UWP应用程序的清单已经请求该权限。然而,有时(可能是自Windows 1809以来)似乎不会自动授予。相反,用户需要从控制面板打开应用程序的高级选项并进行设置 那么,有没有办法检查应用程序是否具有通知用户的权限 这就是我的意思:设置应用程序(点击应用程序)>点击“高级选项”。还请记住,某些应用可能不需要任何权限,因此您可能看不到它们的任何权限。查看MS weather应用程序,它需要两个权限C# 如何检查用户是否已授予应用对图片文件夹的权限?,c#,uwp,windows-store-apps,win-universal-app,C#,Uwp,Windows Store Apps,Win Universal App,我的UWP应用程序的清单已经请求该权限。然而,有时(可能是自Windows 1809以来)似乎不会自动授予。相反,用户需要从控制面板打开应用程序的高级选项并进行设置 那么,有没有办法检查应用程序是否具有通知用户的权限 这就是我的意思:设置应用程序(点击应用程序)>点击“高级选项”。还请记住,某些应用可能不需要任何权限,因此您可能看不到它们的任何权限。查看MS weather应用程序,它需要两个权限 这是迄今为止我找到的最好的解决方案: private async Task<StorageL
这是迄今为止我找到的最好的解决方案:
private async Task<StorageLibrary> TryAccessLibraryAsync(KnownLibraryId library)
{
try
{
return await StorageLibrary.GetLibraryAsync(library);
}
catch (UnauthorizedAccessException)
{
//inform user about missing permission and ask to grant it
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, please reopen it afterwards. " +
"If you Cancel, the app will have limited functionality only.");
var okCommand = new UICommand("OK");
requestPermissionDialog.Commands.Add(okCommand);
var cancelCommand = new UICommand("Cancel");
requestPermissionDialog.Commands.Add(cancelCommand);
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.");
confirmationDialog.Commands.Add(okCommand);
await confirmationDialog.ShowAsync();
//retry
return await TryAccessLibraryAsync(library);
}
}
专用异步任务TryAccessLibraryAsync(KnownLibraryId库)
{
尝试
{
返回wait-StorageLibrary.GetLibraryAsync(library);
}
捕获(未经授权的访问例外)
{
//通知用户缺少权限并请求授予它
MessageDialog requestPermissionDialog=
新建MessageDialog($“应用程序需要访问{library}。”+
“按“确定”打开系统设置并授予此应用权限。”+
“如果应用程序关闭,请稍后重新打开。”+
“如果取消,应用程序将仅具有有限的功能。”);
var-okCommand=new-UICommand(“OK”);
requestPermissionDialog.Commands.Add(okCommand);
var cancelCommand=新的UICommand(“取消”);
requestPermissionDialog.Commands.Add(cancelCommand);
requestPermissionDialog.DefaultCommandIndex=0;
requestPermissionDialog.CancelCommandIndex=1;
var requestPermissionResult=等待requestPermissionDialog.ShowAsync();
if(requestPermissionResult==cancelCommand)
{
//用户选择取消,应用将没有权限
返回null;
}
//打开应用程序设置,允许用户授予我们权限
wait Launcher.launchurisync(新Uri(“ms设置:appsfeatures应用”));
//要重试的确认对话框
var确认对话框=新建消息对话框(
$“请授予此应用程序{library}权限。”);
confirmationDialog.Commands.Add(okCommand);
等待确认Dialog.ShowAsync();
//重试
返回等待TryAccessLibraryAsync(库);
}
}
它首先尝试通过其KnownLibraryId
获取给定的库。如果用户删除了应用程序的权限,则它将失败,出现UnauthorizedAccessException
现在,我们向用户显示一个MessageDialog
,该对话框解释了问题,并要求用户授予应用程序权限
如果用户按下“取消”,该方法将返回null
,因为用户没有授予我们权限
否则,我们将使用特殊的启动URIms Settings:appsfeatures应用程序
(请参阅)启动设置,它将使用权限切换打开应用程序高级设置页面
现在出现了一个不幸的问题-我发现更改权限将在当前强制关闭应用程序。我在第一个对话框中通知用户这一事实。如果这种情况在将来发生变化,那么代码已经为这种替代方案做好了准备-将显示一个新对话框,当权限发生变化时,用户可以确认该对话框,并且该方法将递归调用自身并再次尝试访问库
当然,我建议在应用程序因权限更改而关闭之前保存用户的数据,以便在重新打开应用程序时,数据将保持完整,用户流不会中断
如果你真的依赖此权限来实现其功能,也可以在应用程序启动后立即调用此权限。这样你就知道你或者有访问权,或者用户会在一开始就给你访问权,所以应用程序将被终止这一事实没有什么坏处
更新:我发现这个问题非常有趣,所以我有。这是迄今为止我找到的最好的解决方案:
private async Task<StorageLibrary> TryAccessLibraryAsync(KnownLibraryId library)
{
try
{
return await StorageLibrary.GetLibraryAsync(library);
}
catch (UnauthorizedAccessException)
{
//inform user about missing permission and ask to grant it
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, please reopen it afterwards. " +
"If you Cancel, the app will have limited functionality only.");
var okCommand = new UICommand("OK");
requestPermissionDialog.Commands.Add(okCommand);
var cancelCommand = new UICommand("Cancel");
requestPermissionDialog.Commands.Add(cancelCommand);
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.");
confirmationDialog.Commands.Add(okCommand);
await confirmationDialog.ShowAsync();
//retry
return await TryAccessLibraryAsync(library);
}
}
专用异步任务TryAccessLibraryAsync(KnownLibraryId库)
{
尝试
{
返回wait-StorageLibrary.GetLibraryAsync(library);
}
捕获(未经授权的访问例外)
{
//通知用户缺少权限并请求授予它
MessageDialog requestPermissionDialog=
新建MessageDialog($“应用程序需要访问{library}。”+
“按“确定”打开系统设置并授予此应用权限。”+
“如果应用程序关闭,请稍后重新打开。”+
“如果取消,应用程序将仅具有有限的功能。”);
var-okCommand=new-UICommand(“OK”);
requestPermissionDialog.Commands.Add(okCommand);
var cancelCommand=新的UICommand(“取消”);
requestPermissionDialog.Commands.Add(cancelCommand);
requestPermissionDialog.DefaultCommandIndex=0;
requestPermissionDialog.CancelCommandIndex=1;
var requestPermissionResult=等待requestPermissionDialog.ShowAsync();
if(requestPermissionResult==cancelCommand)
{
//用户选择取消,应用将没有权限
返回null;
}
//打开应用程序设置,允许用户授予我们权限
wait Launcher.launchurisync(新Uri(“ms设置:appsfeatures应用”));
//要重试的确认对话框
var确认对话框=新建消息对话框(
$“请授予此应用程序{library}权限。”);
confirmationDialog.Commands.Add(okCommand);
等待确认Dialog.ShowAsync();
//重试
返回等待TryAccessLibraryAsync(库);
}
}
这是做什么的