如何在多个文本框中的文件行中将文本插入文本框?

jay*_*t55 2 c# textbox text-files winforms

我正在尝试做一些事情,但我没有在谷歌上找到任何东西,因为我不知道怎么说它来得到正确的结果.

我有一个包含9个TextBox控件的Form,以及一个包含9行文本的PlainText文件.

我想点击一个按钮,然后将文本文件中的第一行文本添加到第一个TextBox中,然后将第二行添加到第二个文本框中,依此类推......任何人都可以提供有关如何执行此操作的任何建议?

Don*_*nut 9

试试这个:

using (StreamReader reader = File.OpenText("yourFileName.txt"))
{
    textBox1.Text = reader.ReadLine();
    textBox2.Text = reader.ReadLine();
    textBox3.Text = reader.ReadLine();
    textBox4.Text = reader.ReadLine();
    textBox5.Text = reader.ReadLine();
    textBox6.Text = reader.ReadLine();
    textBox7.Text = reader.ReadLine();
    textBox8.Text = reader.ReadLine();
    textBox9.Text = reader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

编辑:更改使用的解决方案File.OpenText而不是FileStream

  • `File.OpenText`会更简单,但确实如此. (4认同)