如何在 VB 中写入文件中的特定行

How to Write to a Certain Line in a File in VB

我知道如何写入文件,但是,我希望能够写入特定行。我有一个很大的 csv 文件,其中有很多行。

我只能写到最后一行(使用 writeline),但我真的想写到第 x 行,共 5​​0 行。

我该怎么做?

我不知道您是否可以写入文件中的特定行,但如果需要,您可以将行写入列表,然后将列表写入文件

    'Declare your list
    Dim lines As New List(Of String)

    For Each lineToWrite In YourLines
        If toInsert Then
            'In this way you insert a row at the specified index, but this will grow the list not overwrite.
            lines.Insert(indexToInsert, lineToWrite)
        ElseIf toOverwrite Then
            'In this way you overwrite the item at the specified index
            lines(indexToOverwrite) = lineToWrite
        Else
            'Or you can just append it
            lines.Add(lineToWrite)
        End If

    Next

    'This will write the file
    System.IO.File.WriteAllLines("c:\yourfile.txt", lines.ToArray())