0

如何从 .t​​xt 文件中搜索加载和替换数据并显示它

我在 txt.file 中的数据是

1010w23#Mild#UnknwonStreet#001234521

我想用这个来改变它

1010w34#Mild#UnknownStreet#001235421

我很有趣的 Windows 窗体应用程序,我是 C# 的新手

我用这个保存数据

string[] dataEmployee = new string[4];

dataEmployee[0] = txtIdEmployee.Text;
dataEmployee[1] = txtName.Text;
dataEmployee[2] = txtAddress.Text;
dataEmployee[3] = txtContact.Text;

string.Join("#", dataEmployee);

TextWriter writeDataEmployee = new StreamWriter(@"dataemployee.txt", true);
4

2 回答 2

0

尝试这样的事情,尽管没有更多信息,我们无法确定您到底需要什么:

string text = File.ReadAllText(fileName);    //fileName is the path to your txt file
string[] fields = text.Split('#');
fields[0] = fields[0].Replace("23", "34");
fields[3] = fields[3].Replace("45", "54");
txtResult.Text = String.Join("#", fields);    //txtResult is some textbox in your form
于 2013-04-24T08:49:38.647 回答
0

试试下面的方法,它会帮助你...

在您的表单中创建4 Text boxesone button命名,如下所示..

文本框:

txtEmployeeIDFind
txtEmployeeIDReplace
txtContactReplace
txtContactFind

按钮:

btnFindandReplace

并运行表单现在用户在文本框中输入他们的值,如下所示

在此处输入图像描述

Button click event编写如下编码...

   private void btnFindandReplace_Click(object sender, EventArgs e)
    {
        string text = System.IO.File.ReadAllText("dataemployee.txt");
        text = text.Replace(txtEmployeeIDFind.Text, txtEmployeeIDReplace.Text).Replace(txtContactFind.Text, txtContactReplace.Text);
        System.IO.File.WriteAllText("dataemployee.txt", text);
    }
于 2013-04-24T09:01:11.953 回答