我有这个代码
private void button1_Click(object sender, EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
richTextBox3.Text = filetext; // reads all text into one text box
}
}
}
我正在为如何将文本文件的每一行放入不同的文本框或可能将其存储在一个数组中而苦苦挣扎,请有人帮忙!
请您参考如下方法:
File.ReadAllText将读取文件中的所有文本。
string filetext = File.ReadAllText("The file path");
如果要将每一行单独存储在一个数组中,File.ReadAllLines可以做到这一点。
string[] lines = File.ReadAllLines("The file path");
