【发布时间】:2016-05-19 05:31:57
【问题描述】:
我遇到了一个我无法理解的奇怪错误......
我一直在 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);
}
});
}
}
`
【问题讨论】:
标签: xml visual-studio-2015 uwp filestream