4

我很难弄清楚这个错误的原因是什么。我已经FilePicker在 Manifest 中添加了功能,这不像是我在尝试做任何疯狂的事情;只是试图保存到 Documents 文件夹中的子文件夹...

错误:“在 mscorlib.dll 中发生类型为 'System.UnauthorizedAccessException' 的未处理异常
附加信息:访问被拒绝。(来自 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 创建的文件?

4

3 回答 3

4

您无需调用StorageFolder.GetFolderFromPathAsync(savedItem.Path)and sFolder.GetFileAsync(savedItem.FileName)。您必须删除这两行,因为它们会引发异常。您应该使用方法返回的 StorageFile 对象savePicker.PickSaveFileAsync(),因为该对象具有所有权限。然后你可以简单地调用savedItem.OpenAsync(FileAccessMode.ReadWrite).

于 2011-12-16T15:35:24.577 回答
2

您可能没有在应用程序的 appxmanifest 的功能部分中启用“文档库访问”。如果没有此功能,windows 将限制对文件系统的访问。音乐、视频和图片库也有类似的功能。

您已经将“文件选择器”添加到声明部分,这可能不是您想要的。“文件选择器”声明表明,如果其他应用程序调用文件选择器,您的应用程序将被列为可能的文件源。

于 2011-12-07T20:51:51.537 回答
0

我还发现Manifest中添加Video或Picture Libraries访问能力只有在重启Windows 10后才能生效。可能是我电脑的问题,但我觉得值得分享。

于 2016-01-07T10:25:15.587 回答