简体   繁体   English

覆盖.txt文件中的选择行

[英]Overwrite Select Lines in .txt File

I have an test application that takes information from a user and saves it onto a text file. 我有一个测试应用程序,该应用程序从用户那里获取信息并将其保存到文本文件中。 The problem arises to where certain information needs to be inserted within the structure of the list even though the user will encounter the appropriate fields much later on in the document. 问题是,即使用户在文档的后面会遇到适当的字段,也需要在列表的结构中插入某些信息。

在此处输入图片说明

I have decided to go the lazy route and put placeholders for the information I need added in the text file later on, but I have no idea how to selectively overwrite lines. 我决定走懒惰的路线,并稍后在文本文件中添加所需信息的占位符,但是我不知道如何有选择地覆盖行。

Here is the example: 这是示例:

Form 1: 表格1:

在此处输入图片说明

using System.IO;

namespace Debug.NewFolder1
{  
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnNextInfo_Click_1(object sender, EventArgs e)
    {
        System.IO.StreamWriter objwriter;
        objwriter = new System.IO.StreamWriter("Info.txt", true);
        objwriter.Write("nameL=" + tbxLast.Text);
        objwriter.WriteLine();
        objwriter.Write("nameF=" + tbxFirst.Text);
        objwriter.WriteLine();
        objwriter.Write("nameM=" + tbxMiddle.Text);
        objwriter.WriteLine();
        objwriter.Write("numberAge=" + tbxAge.Text);
        objwriter.WriteLine();

        //Occupation
        objwriter.Write("Occupation=");
        objwriter.WriteLine();
        //Certification
        objwriter.Write("Certification=");
        objwriter.WriteLine();

        objwriter.Write("Residence=" + tbxRes.Text);
        objwriter.WriteLine();
        objwriter.Close();

        NewFolder1.Form2 settingsForm = new NewFolder1.Form2();
        settingsForm.Show();
        this.Hide();
        }}}

Form 2: 表格2:

在此处输入图片说明

using System.IO;

namespace Debug.NewFolder1
{
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void btnNextInfo_Click(object sender, EventArgs e)
    {
        System.IO.StreamWriter objwriter;
        objwriter = new System.IO.StreamWriter("Info.txt", true);
        //skip 4 lines            
        objwriter.Write("Occupation=" + tbxOcc.Text);
        objwriter.WriteLine();
        objwriter.Write("Certification=" + tbxCert.Text);
        objwriter.WriteLine();
        //skip 1 line
        objwriter.Close();

        Application.Exit();
    }}}

If you want to look up a specific line and re-write just that line you can do something like this: 如果要查找特定的行并仅重写该行,可以执行以下操作:

        string filePath = ""; //Your file path goes here
        string[] lines = File.ReadAllLines(filePath);
        string newLastName = "Smith";
        string newFirstName = "John";

        for (int i = 0; i < lines.Count(); i++)
        {
            if (lines[i].Contains("nameL="))
                lines[i] = "nameL=" + newLastName;

            if (lines[i].Contains("nameF="))
                lines[i] = "nameF=" + newFirstName;
        }

        File.WriteAllLines(filePath, lines);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM