我遇到了一个我无法理解的奇怪错误......
我一直在UWP应用程序中使用FolderPicker来允许用户选择文件夹,我需要允许将xml文件保存到所选目录中 . 在许多情况下,这将是拇指驱动器的根文件夹 .
当我选择驱动器的子文件夹(“G:\ Newfolder”)时,我当前的代码工作正常,但是当我选择根(“G:\”)时失败
我得到的错误声称我没有正确的访问权限,但是当我意外地错误地转义斜杠时,我得到了同样的错误,所以我不相信错误代码 .
我不可能保存到拇指驱动器的根部吗?我正在制造另一个错误吗?
这是代码:
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);
}
});
}
}
`
1 回答
这不是一个答案,只是一般观察:
无论何时组合路径,都不要手动输入斜线,而是使用斜杠
为您节省一些潜在的荷马辛普森“doh”时刻:)