Reputation: 6940
I am using a FolderPicker
in my Windows universal app to have the user choose a directory, and when they've done that I need to access several different files at known subpaths below that directory. I can choose the directory fine, but when I try to access things at subpaths from there I am still denied access. Is there any way to grant access to multiple files in a parent directory without having the force the user to manually select each file that needs to be accessed?
Here is my code that opens the folder picker and adds the chosen directory to my future access list:
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null) {
StorageApplicationPermissions.FutureAccessList.AddOrReplace("GitDirectory", folder);
PathTextBox.Text = folder.Path;
this.IsPrimaryButtonEnabled = true;
}
(apologies for my naming conventions, I'm new to the Win/C# world so I'm writing in a hodge-podgey way I know)
Here is the code in another class that tries to access a sub path. You can assume that the variable GitDirectory
has been previously assigned the result of calling FutureAccessList.GetFolderAsync("GitDirectory");
because it has:
StorageFile iniFile = await GitDirectory.GetFileAsync(@"datafiles\i18n\en_US.ini");
IniData data = iniParser.ReadFile(iniFile.Path);
It is this final line that finally throws this exception:
Access to the path 'E:\Documents\git\asirra\datafiles\i18n\en_US.ini' is denied.
Thanks for any help in advance
Upvotes: 0
Views: 289
Reputation: 39072
The problem here is that when you have access to a file via a StorageFile
instance at a arbitrary location (user-chosen), you need to use this specific instance to work with the file. You cannot access the file directly by accessing its path (file.Path
).
I have checked the IniParser
source code on GitHub and found out you can use the IniDataParser
API instead, for example the following method:
IniData Parse(StringReader stringReader)
You could read the ini
file, and then pass it to the parser:
var iniFile = await GitDirectory.GetFileAsync(@"datafiles\i18n\en_US.ini");
var text = await FileIO.ReadAllTextAsync(iniFile);
var parser = new IniDataParser();
var data = parser.Parse(text);
Upvotes: 4