0

像这样的文本文件:

id,name,username,password
1,x,js,111
2,y,mm,222

我可以从文件中读取并将其放在 datagridview 上,这是代码

    private void buttonTadd_Click(object sender, EventArgs e)
    {
        FileStream fs = new FileStream(@"C:\Users\HP\Documents\Visual Studio 2008\Projects\BankServer\TellerFile.txt", FileMode.Append);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine(textBoxTNumber.Text +","+ textBoxTName.Text +","+ textBoxTUserName.Text +","+ textBoxTPassword.Text);
        sw.Close();
        dataGridViewTellers.Rows.Clear();
        StreamReader sr = new StreamReader(@"C:\Users\HP\Documents\Visual Studio 2008\Projects\BankServer\TellerFile.txt");
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            dataGridViewTellers.Rows.Add(line.Split(','));
        }
        sr.Close();
    }
4

1 回答 1

0

您可以使用正则表达式模式匹配来阅读它

File.WriteAllText("Path", Regex.Replace(File.ReadAllText("Path"), "[Pattern]", "Replacement"));

这个链接也可能有帮助

于 2013-06-19T04:56:30.420 回答