gpt4 book ai didi

c# - 使用 FileSavePicker 写入文件

转载 作者:太空狗 更新时间:2023-10-30 00:56:09 26 4
gpt4 key购买 nike

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

Error: "An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access is denied. (Exception from 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 创建的文件?

最佳答案

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

关于c# - 使用 FileSavePicker 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8404034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com