Reputation: 245
I'm trying to do some generic database stuff with a UWP at a directory the user specifies, but I am having a nightmare with the access rights.
I've read everything I can find about the folder picker and I'm still not getting gifted access. My understanding was once the user picked a folder I could use that folder as I pleased, but it doesn't seem to be the case.
private Windows.Storage.StorageFolder _fileAccess = null;
private async void Btn_Browse_Click(object sender, RoutedEventArgs e)
{
FolderPicker picker = new FolderPicker
{
ViewMode = PickerViewMode.List,
SuggestedStartLocation = PickerLocationId.ComputerFolder
};
picker.FileTypeFilter.Add("*");
_fileAccess = await picker.PickSingleFolderAsync();
if (_fileAccess == null)
{
return;
}
Tbx_Directory.Text = _fileAccess.Path;
StorageApplicationPermissions.FutureAccessList.
AddOrReplace("PickedFolderToken", _fileAccess);
string[] dataBases = Directory.GetFiles(_fileAccess.Path, @"*.db");
foreach (string file in dataBases ?? Enumerable.Empty<string>())
{
LBxV_Databases.Items.Add(file);
}
}
I get the access violation on the Directory use.
Upvotes: 0
Views: 197
Reputation: 7737
You are using FutureAccessList
, this is a great choice, but there is a problem with the way you use it.
Here is the way to get the saved StorageFolder
:
public async Task<StorageFolder> GetFolderFromAccessList(string tokenName)
{
var folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(tokenName);
return folder;
}
With the FolderPicker
, you can get the StorageFolder
object. But saving this object to the FutureAccessList
does not allow you to access the folder with the path. You can only get the saved folder object by the Token which save in the FutureAccessList
.
Because the UWP application is a sandbox application, when you access the database, I recommend that you save the database file in the application's local directory, such as ApplicationData.LocalFolder
. You can't directly access the external file without adding special Capability.
You can find an official application example provided by Microsoft here, which demonstrates how to access files/folders persistently.
Best regards.
Upvotes: 2