我有這樣的代碼讀取文件內容到一個數組
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.ReadAllLines'而不是'File.ReadAllText'? –
你不應該使用dialog.OpenFile,然後File.ReadAllText,對話框有一個選項來驗證文件是否存在,然後就像@JonSkeet所說的那樣使用ReadAllLines。 –