如何将.txt文件中的文本显示到文本框中 [英] How to display a text from a .txt file in to a textbox
本文介绍了如何将.txt文件中的文本显示到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要读取带有扩展名.txt的文本文件,然后让它显示在C#程序中的文本框中。
我创建的代码只显示我输入的路径但是没有加载在txtfile的文本中。
这是我到目前为止的代码。
任何想法关于如何加载文件中的文本而不是
I need to read a textfile with extention .txt and let is show into a textbox within a C# program.
The code I created only show me the path I entered but doesn''t load in the text from the txtfile.
This is the code I got so far.
Any ideas on how to load in the text in the file instead
private void textBox1_TextChanged(object sender, EventArgs e)
{
string value = @"C:\Users\Respawn\Documents\AllTxtFiles.txt";
StringReader OpenFile = new StringReader(value) ;
textBox1.Text = OpenFile.ReadToEnd();
}
推荐答案
需要使用流阅读器
Need to use a Stream Reader
StreamReader OpenFile = new StreamReader(value);
textBox1.Text = OpenFile.ReadToEnd();
OpenFile.Close();
您好,
作为解决方案1的替代方案,您可以使用< a href =http://msdn.microsoft.com/en-us/library/ms143368.aspx> File.ReadAllText [ ^ ]从文件中读取所有文本:
Hi,
As alternative to Solution 1, you can use File.ReadAllText[^] to read all text from a file:
string s = File.ReadAllText(@"C:\Users\Respawn\Documents\AllTxtFiles.txt");
textBox1.Text = s;
希望这有帮助。
Hope this helps.
这篇关于如何将.txt文件中的文本显示到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文