Reputation: 71
I've come to a strange error I cannot understand...
I've been using the FolderPicker in a UWP app to allow a user to choose a folder, and I need to allow an xml file to be saved to the directory chosen. In many cases, this would be the root folder of a thumb drive.
The current code I have works fine when I choose a subfolder of the drive ("G:\Newfolder"), but fails when I choose the root ("G:\")
The error that I get claims that I do not have proper access, but I get the same error when I had accidentally escaped the slashes incorrectly, so I'm not trustful of the error code.
Is it impossible for me to save to the root of a thumb drive? Is there another mistake I'm making?
Here is the code:
private async void buttonSaveToRoot_Click(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.ViewMode = PickerViewMode.List;
folderPicker.FileTypeFilter.Add(".xml");
//OK, imagine me picking the root of my thumb drive - "G:\"
StorageFolder pickedFolder = await folderPicker.PickSingleFolderAsync();
if (pickedFolder != null)
{
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", pickedFolder);
XElement xml = new XElement("Food");
xml.Add (new XElement("Oranges", new XAttribute("Type", "Fruit")));
xml.Add(new XElement("Apples", new XAttribute("Type", "Fruit")));
xml.Add(new XElement("Potatoes", new XAttribute("Type", "Vegetables")));
xml.Add(new XElement("Carrots", new XAttribute("Type", "Vegetables")));
string savePath = pickedFolder.Path + @"\test.xml";
savePath = savePath.Replace(@"\\", @"\") ;
await Task.Run(() =>
{
Task.Yield();
using (FileStream fs = File.Create(savePath))
{
xml.Save(fs);
}
});
}
}
`
Upvotes: 1
Views: 81
Reputation: 3222
This is not an answer, just a general observation:
Whenever you're combining paths, do not manually enter slashes, but instead, use
var savePath = Path.Combine(pickedFolder.Path, "test.xml");
Saves you a few potential Homer Simpson "doh" moments :)
Upvotes: 1