C#窗体读取一个txt文件(含两列数字,两列数字用空格分开),把这两列数字给两个textbox(各一列),详细步骤
a 1
b 2
c 3
d 4
//选择文件
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "文本文件|*.txt";
if (open.ShowDialog()==DialogResult.OK)
{
string filepath = open.FileName;//文件路径
textBoxfilepath.Text = filepath;
StreamReader reader = new StreamReader(filepath, Encoding.Default);
char[] s = {' '};//分隔符为空格
StringBuilder onecolum = new StringBuilder();//存储第一列数据
StringBuilder towcolum = new StringBuilder();//存储第二列数据
string reddate = reader.ReadLine();//读取一行数据
while(!string.IsNullOrEmpty(reddate))//如果数据不为空
{
string[] record=reddate.Split(s);
onecolum.Append(record[0]);
towcolum.Append(record[1]);
reddate = reader.ReadLine();//读取下一行
}
textBox1clm.Text = onecolum.ToString();
textBox2clm.Text = towcolum.ToString();
}
}
textBoxfilepath.Text = filepath;中的textBoxfilepath是什么啊 没有申明啊
textBoxfilepath是第一个文本框的名字 用来显示你选择文件的路径