【问题标题】:Overwrite Select Lines in .txt File覆盖 .txt 文件中的选择行
【发布时间】:2018-02-02 15:21:12
【问题描述】:

我有一个测试应用程序,它从用户那里获取信息并将其保存到文本文件中。问题出现在需要在列表结构中插入某些信息的位置,即使用户稍后会在文档中遇到适当的字段。

我决定走懒惰的路线,并将占位符用于稍后在文本文件中添加的信息,但我不知道如何选择性地覆盖行。

示例如下:

表格一:

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

表格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();
    }}}

【问题讨论】:

  • 不要小看File.ReadAllLinesFile.WriteAllLines的力量,也许它对你更有意义。
  • 您是在处理小文件还是非常大的文件(内存可能是个问题)?
  • 必须是原始文本吗?或者你可以使用 XML 吗?如果您使用 XML,您可以将其序列化/反序列化为易于操作的对象,然后在完成后仅调用一次反序列化/写入。这个问题和答案展示了如何使用 xDocument 库仅操作 xml 文档中的 1 个对象:stackoverflow.com/questions/18508765/… 您可以使用常规 XML 做同样的事情,在这两种情况下,如果您有一个大文件,我建议您检查性能,因为我相信他们都会重写整个文件。

标签: c# visual-studio-2015


【解决方案1】:

如果您想查找特定行并仅重写该行,您可以执行以下操作:

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

【讨论】:

  • 抱歉久等了,有时间我会测试一下,如果不行我会重做OP并添加图片
猜你喜欢
  • 2021-07-19
  • 2012-11-23
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 2012-04-19
  • 1970-01-01
相关资源
最近更新 更多