C#在文本框中打开文本文件 [英] C# opening a text file in a text box
本文介绍了C#在文本框中打开文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何从使用打开文件对话框打开的文件中提取文本并将该文本写到富文本框中.
I would like to know how I can bring text from a file that I open using open file dialogue and write that text to a rich text box.
推荐答案
试试:
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string s = File.ReadAllText(ofd.FileName);
myRichTextBox.Text = s;
}
您是否要打开一个扩展名为".txt"但其内容为RTF格式的文件?而且...可能...您希望打开的RTF内容显示为...作为RTF ...,即带有颜色等.
或者,也许您想为原始文本"添加颜色或其他内容?
如果第一个问题的答案是是",请检查用于RTF内容的"LoadFile"的变体,例如:RichTextBox.LoadFile方法(Stream,RichTextBoxStreamType)[
Is it your intent to open a file that happens to have the ''.txt'' extension, but whose contents are in the RTF format ? And ... possibly ... you want that opened RTF content to display ... as RTF ... i.e., with color, etc. ?
Or, perhaps you want to add color, or whatever, to the ''raw text'' ?
If the answer to the first question is ''yes,'' then please examine the variations of ''LoadFile meant to be used with RTF content such as: RichTextBox.LoadFile Method (Stream, RichTextBoxStreamType)[^].
使用代码像这样:
use code like this :
Dim srRead As New System.IO.StreamReader(openFileDialog1.FileName)
Dim strFileText As String = ""
strFileText = srRead.ReadToEnd
srRead.Close()
TextBox1.Text = strFileText
有关更多信息:
http://www.authorcode.com/how-to -load-text-file-into-a-textbox-in-net/ [
for more information :
http://www.authorcode.com/how-to-load-text-file-into-a-textbox-in-net/[^]
这篇关于C#在文本框中打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文