c#在特定行号处将一行写入文本文件

c#在特定行号处将一行写入文本文件,c#,io,C#,Io,我在网上搜索,但没有找到正确的例子 string path ="<Path of file>" List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>(); //give the line to be inserted lines[10]="New String"; System.IO.File.WriteAllLines(path, l

我在网上搜索,但没有找到正确的例子

    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);
目标是具有以下功能:

private void InsertLine(string source, string position, string content)
    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);
并使用StreamWriter写入文件,这样就不需要读取所有行,因为文件可能很大

    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);
到目前为止,我的职能是:

    private void InsertLine(string source, string position, string content)
    {
        if (!File.Exists(source))
            throw new Exception(String.Format("Source:{0} does not exsists", source));

        var pos = GetPosition(position);
        int line_number = 0;
        string line;
        using (var fs = File.Open(source, FileMode.Open, FileAccess.ReadWrite))
                {
                    var destinationReader = new StreamReader(fs);
                    var writer = new StreamWriter(fs);
                    while (( line = destinationReader.ReadLine()) != null)
                    {
                      if (line_number == pos)
                        {
                            writer.WriteLine(content);
                            break;
                        }                            
                        line_number++;
                    }
                }
    }
    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);

该函数在文件中不起作用,因为什么都没有发生。

不能只在文件中插入一行。文件是一个字节序列

    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);
您需要:

    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);
  • 写下前面的所有行
  • 写下要插入的行
  • 写下下面所有的行
以下是一些基于您的未经测试的代码:

private void InsertLine(string source, string position, string content)
{
    if (!File.Exists(source))
        throw new Exception(String.Format("Source:{0} does not exsists", source));

    // I don't know what all of this is for....
    var pos = GetPosition(position);
    int line_number = 0;
    string line;

    using (var fs = File.Open(source, FileMode.Open, FileAccess.ReadWrite))
    {
        var destinationReader = new StreamReader(fs);
        var writer = new StreamWriter(fs);
        while (( line = destinationReader.ReadLine()) != null)
        {
            writer.WriteLine(line);    // ADDED: You need to write every original line

            if (line_number == pos)
            {
                writer.WriteLine(content);
                // REMOVED the break; here. You need to write all following lines
            }

            line_number++;    // MOVED this out of the if {}. Always count lines.

        }

    }
}
    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);

然而,这可能不会像预期的那样起作用。您正在尝试写入正在读取的同一文件。您应该打开一个新的(临时)文件,执行复制+插入,然后移动/重命名临时文件以替换原始文件。

此代码将帮助您插入所需行号上的文本

    string path ="<Path of file>"
    List<string> lines = System.IO.File.ReadAllLines(path).ToList<string>();
    //give the line to be inserted
    lines[10]="New String";
    System.IO.File.WriteAllLines(path, lines);
string path=“”
List lines=System.IO.File.ReadAllLines(path.ToList();
//给出要插入的行
行[10]=“新字符串”;
System.IO.File.writeAllines(路径,行);

我不认为在不读取和写回文件的其余行的情况下“插入”一行是可能的。文本文件不仅仅是一组链接行,在这一点之后的所有内容都需要移动。幸运的是,没有发生任何事情,因为您的代码会将文件截断为一行。那个职位的代码是什么?这是令人费解的。您是否调试了代码并检查它是否实际到达第行:writer.WriteLine(content);另外,wrap var writer=新的StreamWriter(fs);在一个using行中,即使用(var writer=newstreamwriter(fs)){}我想到了一些东西,告诉我前面的所有行是什么。在处理大文件时,这在性能方面可能很重要。@Sinatr非常好。我试图简单地修改操作代码,以证明它做错了什么。也许这个问题最好以你链接的问题的副本来结束。我确实理解你的意思,但是这个概念仍然需要阅读整个文件。这可以通过seek之类的技术来实现吗?请按照上面sinatr发布的链接进行操作。问题说明是否有一种方法可以在不阅读整个文件的情况下编写它。原因是它的可用内存更大