我正在编写一个需要获得访问文本文件cuz权限的应用程序,但没有获得许可,它会引发异常“访问被拒绝”。 我在Package.appxmanifest中添加了特定行
import json
def servers_from_json(file_name):
with open(file_name, 'r') as f:
data = json.loads(f.read())
servers = [{'asn': item['data']['resource'], 'resource': item['data']['allocations'][0]['asn_name']} for item in data]
return servers
servers = servers_from_json('servers.json')
print(servers) # => [{'asn': '185.230.125.0/24', 'resource': 'RO-M247EUROPE-OCT-20171108'}, {'asn': '45.9.249.0/24', 'resource': 'RO-M247-APR1901-20190423'}]
还有
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities
"IgnorableNamespaces="uap mp rescap"
但是仍然不起作用。还有其他方法可以使用选择器访问特定文件吗?
答案 0 :(得分:2)
是的,该行为在2018年4月至2018年10月的发行版之间发生了更改,并且默认值为“禁用”。这是隐私限制-我们非常注重维护用户的隐私。有关此文档的信息是最新的:https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions#accessing-additional-locations。从现在开始,如果您想检测该设置是启用还是禁用,则可以简单地尝试访问一些文件/文件夹,如果启用,此设置将向您授予权限;如果禁用,则拒绝访问(例如,“ C:\ ”)。如果禁用,则可以在“文件系统”隐私页面上启动“设置”应用程序。例如:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
try
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"C:\");
// do work
}
catch
{
MessageDialog dlg = new MessageDialog(
"It seems you have not granted permission for this app to access the file system broadly. " +
"Without this permission, the app will only be able to access a very limited set of filesystem locations. " +
"You can grant this permission in the Settings app, if you wish. You can do this now or later. " +
"If you change the setting while this app is running, it will terminate the app so that the " +
"setting can be applied. Do you want to do this now?",
"File system permissions");
dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(InitMessageDialogHandler), 0));
dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(InitMessageDialogHandler), 1));
dlg.DefaultCommandIndex = 0;
dlg.CancelCommandIndex = 1;
await dlg.ShowAsync();
}
}
private async void InitMessageDialogHandler(IUICommand command)
{
if ((int)command.Id == 0)
{
await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-broadfilesystemaccess"));
}
}
答案 1 :(得分:0)
您可以使用AppCapability类上的方法来查询应用功能的状态并请求访问,这可能会提示用户,具体取决于随时间而变化的许多地缘政治约束。 CheckAccess()方法将在调用时提供功能的状态。该应用程序可以根据结果调整其行为。理想情况下,如果应用程序在缩减功能模式下运行,它将向用户显示某种指示,并带有指向更多详细信息和有关如何启用完整功能的说明的链接。