Sup*_*Sup 1 c# text openfiledialog
我有这个代码
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在努力如何将文本文件的每一行放到不同的文本框或可能将它存储在一个数组中,有人可以帮忙吗!
File.ReadAllText将读取文件中的所有文本。
string filetext = File.ReadAllText("The file path");
Run Code Online (Sandbox Code Playgroud)
如果您想将每一行单独存储在一个数组中,File.ReadAllLines可以做到这一点。
string[] lines = File.ReadAllLines("The file path");
Run Code Online (Sandbox Code Playgroud)