我有这个代码
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");