C# 在文本文件中的特定位置添加新行。

C# 在文本文件中的特定位置添加新行。,c#,io,streamwriter,C#,Io,Streamwriter,我试图在文件中添加一行特定的文本。特别是在两个边界之间 如果我想在item1的边界之间添加一条线,它会是什么样子的示例: [item1] 2550 coins 995 200000 7 2550 coins 995 200000 7 2550 coins 995 200000 7 2550 coins 995 200000 7 2550 coins 995 200000 7 //Add a line here in between the specific boundaries [/item1]

我试图在文件中添加一行特定的文本。特别是在两个边界之间

如果我想在item1的边界之间添加一条线,它会是什么样子的示例:

[item1]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
//Add a line here in between the specific boundaries
[/item1]
[item2]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 8
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item2]
[item3]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item3]
这是我到目前为止尝试过的,但是它根本不正确。它不断地说,该文件正在被读者使用,所以它不能被作者编辑,当我让它工作时,它清除了整个文档

public void createEntry(String npcName)
{
    String line;
    String fileName = "Drops.de";
    StreamWriter streamWriter = new StreamWriter(fileName);
    StreamReader streamReader = new StreamReader(fileName);
    line = streamReader.ReadLine();
    if (line == ("[" + npcName + "]"))
    {
        streamReader.ReadLine();
        streamWriter.WriteLine("Test");
    }
}

我还想知道如何在文档末尾写行。

您不应该打开文件两次,请尝试以下方法:

public void createEntry(String npcName)
{
    String line;
    String fileName = "Drops.de";
    StreamWriter streamWriter = new StreamWriter(fileName);
    StreamReader streamReader = new StreamReader(fileName);
    line = streamReader.ReadLine();
    if (line == ("[" + npcName + "]"))
    {
        streamReader.ReadLine();
        streamWriter.WriteLine("Test");
    }
}
FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
StreamWriter streamWriter = new StreamWriter(fileStream);
StreamReader streamReader = new StreamReader(fileStream);
另一个想法是插入行的逻辑,可能更简单的方法是将数据逐行复制到新文件中,在需要时插入新部件并继续。或者在记忆中这样做


要在末尾添加行,您可以使用FileMode。追加或执行您自己的搜索

这将在需要的位置添加行。(确保已添加
使用System.IO;
使用System.Linq;

试试这个方法

using System.IO;
using System.Linq;

    /// <summary>
    /// Add a new line at a specific position in a simple file        
    /// </summary>
    /// <param name="fileName">Complete file path</param>
    /// <param name="lineToSearch">Line to search in the file (first occurrence)</param>
    /// <param name="lineToAdd">Line to be added</param>
    /// <param name="aboveBelow">insert above(false) or below(true) of the search line. Default: above </param>
    internal static void insertLineToSimpleFile(string fileName, string lineToSearch, string lineToAdd, bool aboveBelow = false)
    {          
        var txtLines = File.ReadAllLines(fileName).ToList();
        int index = aboveBelow?txtLines.IndexOf(lineToSearch)+1: txtLines.IndexOf(lineToSearch);
        if (index > 0)
        {
            txtLines.Insert(index, lineToAdd);
            File.WriteAllLines(fileName, txtLines);
        }
    }
使用System.IO;
使用System.Linq;
/// 
///在简单文件中的特定位置添加新行
/// 
///完整文件路径
///要在文件中搜索的行(第一次出现)
///要添加的行
///在搜索行的上方(false)或下方(true)插入。默认值:以上
内部静态void insertLineToSimpleFile(字符串文件名、字符串lineToSearch、字符串lineToAdd、bool-upper-below=false)
{          
var txtLines=File.ReadAllLines(文件名).ToList();
int index=上下?txtLines.IndexOf(lineToSearch)+1:txtLines.IndexOf(lineToSearch);
如果(索引>0)
{
txtLines.Insert(索引、行到添加);
writeAllines(文件名,txtLines);
}
}

像在上一个问题中一样阅读文件,并在添加新行的同时写入新文件。我添加了我尝试过的内容,我认为这离这很遥远,唯一的问题是它一直告诉我文件正在使用中,在“使用(file.Create(fileName)){}奇怪,它对我来说很好。您是否有其他使用该文件的代码?您不需要任何
FileStream
StreamWriter
StreamReader
来执行此操作。啊,对不起。我的另一个方法影响了你的方法。不管怎么说,它现在似乎工作得很好,谢谢:)很高兴你让它工作了。我希望这也涵盖了您的第二个问题,您可以使用此代码在末尾添加一行。我不建议使用此代码在结尾追加文本。因为它必须在内存中完全加载整个文件。相反,我建议:使用var fileSteam=File.AppendText(path)。fileSteam.WriteLine(newItem);