不使用picker(C#-visualstudio-UWP)读取/写入RichEditBox(.rtf)文件

不使用picker(C#-visualstudio-UWP)读取/写入RichEditBox(.rtf)文件,c#,visual-studio,win-universal-app,C#,Visual Studio,Win Universal App,我正在努力用C#创建我的第一个UWP程序。我在读/写.rtf文件时遇到问题,无法找到解决方案,也无法在论坛中找到解决方案 我的程序是一个阅读和撰写日报的应用程序。每日日记账分录在RichEditBox中显示和编辑。RichEditBox旁边是CalendarDatePicker 当CalendarDatePicker值更改时,程序将基于CalendarDatePicker日期创建文件名。例如,如果CalendarDatePicker的值为7/22/2018,我的程序会将其转换为以下文件名:“20

我正在努力用C#创建我的第一个UWP程序。我在读/写.rtf文件时遇到问题,无法找到解决方案,也无法在论坛中找到解决方案

我的程序是一个阅读和撰写日报的应用程序。每日日记账分录在RichEditBox中显示和编辑。RichEditBox旁边是CalendarDatePicker

当CalendarDatePicker值更改时,程序将基于CalendarDatePicker日期创建文件名。例如,如果CalendarDatePicker的值为7/22/2018,我的程序会将其转换为以下文件名:“2018_7_22.rtf”,然后将其分配给名为fileName的字符串

以上所有操作均按预期进行。以下是我遇到的问题:

当CalendarDatePicker的值更改时,程序应该立即将.rtf文件(如果存在)从存储器加载到RichEditBox中。而且,当我按下保存按钮时,程序被激活 应该将RichEditBox内容保存到存储器中。读取和写入文件都应该 使用从CalendarDatePicker值派生的名称,而不是打开任何一个值的选择器 打开或保存文件

我找不到任何关于如何在不使用选择器的情况下为RichEditBox读写.rtf文件的文档。我不想使用选取器,因为我希望只能根据基于CalendarDatePicker当前值的文件名来读取或写入给定的日记账条目

以下是我试图做的:

字符串filePath=Windows.Storage.ApplicationData.Current.LocalFolder.toString; //此操作返回错误:“无法将方法组“ToString”转换为非委托类型字符串”

字符串fileName=“2018\u 22\u 7.rtf” //这是我的程序根据CalendarDatePicker的值创建的字符串示例

我问题的最后是RichEditBox文档中的代码。我想去掉使用选择器的部分,并替换以下行: Windows.Storage.StorageFile file=wait savePicker.PickSaveFileAsync(); 与: Windows.Storage.StorageFile文件=文件路径+文件名

是否可以这样做,还是我必须使用RichEditBox的选择器?
我将非常感谢在解决这个问题上能得到的任何帮助。我是 从挫折中融化!谢谢大家!

以下是RichEditBox文档中的代码:

私有异步无效保存按钮\u单击(对象发送方,路由目标) { Windows.Storage.Pickers.FileSavePicker savePicker=new Windows.Storage.Pickers.FileSavePicker(); savePicker.SuggestedStartLocation=Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary

// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Rich Text", new List<string>() { ".rtf" });

// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document";

Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
    // Prevent updates to the remote version of the file until we
    // finish making changes and call CompleteUpdatesAsync.
    Windows.Storage.CachedFileManager.DeferUpdates(file);
    // write to file
    Windows.Storage.Streams.IRandomAccessStream randAccStream =
        await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

    editor.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf, randAccStream);

    // Let Windows know that we're finished changing the file so the
    // other app can update the remote version of the file.
    Windows.Storage.Provider.FileUpdateStatus status = await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
    if (status != Windows.Storage.Provider.FileUpdateStatus.Complete)
    {
        Windows.UI.Popups.MessageDialog errorBox =
            new Windows.UI.Popups.MessageDialog("File " + file.Name + " couldn't be saved.");
        await errorBox.ShowAsync();
    }
}
//用户可以将文件保存为的文件类型下拉列表
savePicker.FileTypeChoices.Add(“富文本”,新列表(){.rtf});
//如果用户未键入文件名或未选择要替换的文件,则为默认文件名
savePicker.SuggestedFileName=“新建文档”;
Windows.Storage.StorageFile file=wait savePicker.PickSaveFileAsync();
如果(文件!=null)
{
//阻止更新文件的远程版本,直到我们
//完成更改并调用CompleteUpdatesAsync。
DeferUpdates(文件);
//写入文件
Windows.Storage.Streams.irandomaccesstream和Accstream=
wait file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
editor.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf,randAccStream);
//让Windows知道我们已完成更改文件,以便
//其他应用程序可以更新文件的远程版本。
Windows.Storage.Provider.FileUpdateStatus状态=等待Windows.Storage.CachedFileManager.CompleteUpdatesSync(文件);
if(状态!=Windows.Storage.Provider.FileUpdateStatus.Complete)
{
Windows.UI.Popups.Message对话框错误=
新建Windows.UI.Popups.MessageDialog(“文件”+文件名+“无法保存”);
等待errorBox.ShowAsync();
}
}
}

默认情况下,通用Windows应用程序(应用程序)可以访问某些文件系统位置

在Windows版本17134之前,如果您不想使用访问文件,您的UWP应用程序只能访问应用程序安装目录中的文件、应用程序数据位置、可移动设备以及UWP应用程序可以使用特定功能访问的一些位置,如音乐和图片库。但是您不能写入应用程序安装目录。在这种情况下,您可以尝试将文件保存在除应用程序安装目录之外的上述位置。例如,然后您可以使用以下代码创建和获取文件。有关更多详细信息,请参阅

StorageFolder localFolder = ApplicationData.Current.LocalFolder;
//Createa file
StorageFile fileToSave = await localFolder.CreateFileAsync("YourFileName");
//Get file
StorageFile file = await localFolder.GetFileAsync("YourFileName");
如果您的应用程序目标版本为17134及更高版本,您可以使用
broadFileSystemAccess
功能访问用户有权访问的所有文件,此功能适用于命名空间中的API,您可以使用以下代码中的路径获取文件,下面是一个示例

这显示了负载。您可能可以从这里找到保存的方法。
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);