搜索加载并将.txt文件中的数据替换为C#并在C#中显示

时间:2013-04-24 08:43:25

标签: c#

如何搜索加载并将.txt文件中的数据替换为并显示

我在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);

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试如下,它会帮助你...

在您的表单中创建4 Text boxesone button,并将其命名如下。

TextBoxes:

txtEmployeeIDFind
txtEmployeeIDReplace
txtContactReplace
txtContactFind

按钮:

btnFindandReplace

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

enter image description here

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);
    }