我很难弄清楚这个错误的原因是什么。我在报表中添加了FilePicker功能,这并不是我想做什么疯狂的事情,我只是试图保存到文档文件夹中的一个子文件夹.
错误:“System.UnauthorizedAccessException类型的未处理异常在mscorlib.dll中发生
其他信息:访问被拒绝。(HRESULT例外: 0x80070005 (E_ACCESSDENIED))“
我已经确认我的用户帐户是管理员,并且它对文件夹和文件有完全的控制。但我不确定我还能尝试什么。
public void NewBTN_Click(object sender, RoutedEventArgs e)
{
var mbox = new MessageDialog("Would you like to save changes before creating a new Note?", "Note+ Confirmation");
UICommand YesBTN = new UICommand("Yes", new UICommandInvokedHandler(OnYesBTN));
UICommand NoBTN = new UICommand("No", new UICommandInvokedHandler(OnNoBTN));
mbox.Commands.Add(YesBTN);
mbox.Commands.Add(NoBTN);
mbox.DefaultCommandIndex = 1;
mbox.ShowAsync().Start();
}
async void OnYesBTN(object command)
{
this.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) =>
{
// User clicked yes. Show File picker.
HasPickedFile = true;
}, this, null);
if (HasPickedFile)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Cascading Stylesheet", new List<string>() { ".css" });
savePicker.FileTypeChoices.Add("Hypertext Markup Language", new List<string>() { ".html" });
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
// Default extension if the user does not select a choice explicitly from the dropdown
savePicker.DefaultFileExtension = ".txt";
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Note";
StorageFile savedItem = await savePicker.PickSaveFileAsync();
if (null != savedItem)
{
// Application now has read/write access to the saved file
StorageFolder sFolder = await StorageFolder.GetFolderFromPathAsync(savedItem.Path);
try
{
StorageFile sFile = await sFolder.GetFileAsync(savedItem.FileName);
IRandomAccessStream writeStream = await sFile.OpenAsync(FileAccessMode.ReadWrite);
IOutputStream oStream = writeStream.GetOutputStreamAt(0);
DataWriter dWriter = new DataWriter(oStream);
dWriter.WriteString(Note.Text);
await dWriter.StoreAsync();
oStream.FlushAsync().Start();
// Should've successfully written to the file that Windows FileSavePicker had created.
}
catch
{
var mbox = new MessageDialog("This file does not exist.", "Note+ Confirmation");
UICommand OkBTN = new UICommand("Ok", new UICommandInvokedHandler(OnOkBTN));
mbox.Commands.Add(OkBTN);
mbox.DefaultCommandIndex = 1;
mbox.ShowAsync().Start();
}
}
}
}
public void OnOkBTN(object command)
{
this.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) =>
{
// Do something here.
}, this, null);
}
public void OnNoBTN(object command)
{
this.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, (s, a) =>
{
// Don't save changes. Just create a new blank Note.
Note.Text = String.Empty;
}, this, null);
}如何将其写入由FileSavePicker创建的文件?
发布于 2011-12-16 15:35:24
你不需要打电话给StorageFolder.GetFolderFromPathAsync(savedItem.Path)和sFolder.GetFileAsync(savedItem.FileName)。必须删除这两行代码,因为它们会引发异常。您应该使用方法StorageFile返回的savePicker.PickSaveFileAsync()对象,因为该对象具有所有权限。然后您可以简单地调用savedItem.OpenAsync(FileAccessMode.ReadWrite)。
发布于 2011-12-07 20:51:51
您可能在应用程序应用程序的应用程序清单的功能部分中没有启用“文档库访问”。如果没有此功能,windows将限制对文件系统的访问。音乐、视频和图片库也有类似的功能。
您已经将“文件选择器”添加到声明部分,这可能不是您想要的。“文件选择器”声明表明,如果其他应用程序调用文件选择器,您的应用程序将被列为可能的文件源。
发布于 2016-01-07 10:25:15
我还发现,只有在重启Windows 10之后,才能在Manifest中添加视频或图片库访问功能。也许这对我的计算机来说是个问题,但我认为值得分享。
https://stackoverflow.com/questions/8404034
复制相似问题