WPF Richtextbox 以纯文本形式打开 RTF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14443295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Richtextbox open RTF file as plain text
提问by MCSharp
I am trying to open a file to view the contents as plain text inside a RichTextboxon a Buttonclick. Nothing seems to work properly.
我正在尝试打开一个文件,以RichTextbox在Button单击时以纯文本形式查看内容。似乎什么都不能正常工作。
private void loadFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.FileName = "Document";
openFile1.DefaultExt = "*.*";
openFile1.Filter = "All Files|*.*|Rich Text Format|*.rtf|Word Document|*.docx|Word 97-2003 Document|*.doc";
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFile1.FileName.Length > 0)
{
//richTextbox1.Document.ContentStart = File.ReadAllText(openFile1.FileName);
}
}
I am using WPF and the LoadFile method doesn't work. I'd like to be able to select a file from the OpenFileDialogand have it be loaded as plain text inside the RichTextbox. Without seeing added code from file formats.
我正在使用 WPF 并且 LoadFile 方法不起作用。我希望能够从 中选择一个文件OpenFileDialog并将其作为纯文本加载到RichTextbox. 没有看到文件格式中添加的代码。
The behavior I'd like is similar to opening an .rtf, selecting all text, and pasting that result into the RichTextbox. How can I do that with a button click?
我想要的行为类似于打开 .rtf,选择所有文本,然后将该结果粘贴到 .rtf 文件中RichTextbox。我怎样才能通过单击按钮来做到这一点?
回答by Abdusalam Ben Haj
Use TextRangeand FileStream
使用TextRange和FileStream
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{
TextRange range;
System.IO.FileStream fStream;
if (System.IO.File.Exists(openFile1.FileName))
{
range = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);
fStream = new System.IO.FileStream(openFile1.FileName, System.IO.FileMode.OpenOrCreate);
range.Load(fStream, System.Windows.DataFormats.Rtf );
fStream.Close();
}
}
回答by paul
Have you tried using richTextbox1.AppendText(File.ReadAllText(openFile1.FileName))?
你试过使用richTextbox1.AppendText(File.ReadAllText(openFile1.FileName))吗?
回答by jimbojones
Similar to @AbZy, you need to clear the formatting first:
与@AbZy 类似,需要先清除格式:
private void loadFile_Click(object sender, RoutedEventArgs routedEventArgs)
{
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.FileName = "Document";
openFile1.DefaultExt = "*.*";
openFile1.Filter = "All Files|*.*|Rich Text Format|*.rtf|Word Document|*.docx|Word 97-2003 Document|*.doc";
if (openFile1.ShowDialog() == true)
{
var range = new TextRange(rtf.Document.ContentStart, rtf.Document.ContentEnd);
using (var fStream = new FileStream(openFile1.FileName, FileMode.OpenOrCreate))
{
// load as RTF, text is formatted
range.Load(fStream, DataFormats.Rtf);
fStream.Close();
}
// clear the formatting, turning into plain text
range.ClearAllProperties();
}
}

