VB.NET在文本文件的指定行后添加文本

VB.NET在文本文件的指定行后添加文本,vb.net,Vb.net,如何使用vb.net在文本文件的指定行之后(或之前)添加文本? 提前感谢要在文本文件第5行之后添加文本,请拨打: AppendAtPosition("C:\Temp\test.txt", 5, "contenttoappend") 要在文本文件第5行之前添加文本,只需调用 AppendAtPosition("C:\Temp\test.txt", 4, "contenttoappend") 需要此功能: 如果文本文件的行数少于预期,则内容将添加到最后一行之后 Private Sub App

如何使用vb.net在文本文件的指定行之后(或之前)添加文本?

提前感谢

要在文本文件第5行之后添加文本,请拨打:

AppendAtPosition("C:\Temp\test.txt", 5, "contenttoappend")
要在文本文件第5行之前添加文本,只需调用

AppendAtPosition("C:\Temp\test.txt", 4, "contenttoappend")

需要此功能:
如果文本文件的行数少于预期,则内容将添加到最后一行之后

Private Sub AppendAtPosition(ByVal ltFilePath As String, ByVal liAppendLine As Integer, ByVal ltAppendLine As String)

    Dim ltFileContents As String = ""
    Dim lReader As StreamReader = New StreamReader(ltFilePath)
    Dim liRow As Integer = 0

    While Not lReader.EndOfStream
      ltFileContents &= lReader.ReadLine
      If liRow = liAppendLine Then
        ltFileContents &= ltAppendLine
        Exit While
      End If
      liRow += 1
    End While
    If liAppendLine >= liRow Then
      ltFileContents &= ltAppendLine
    End If
    File.WriteAllText(ltFilePath, ltFileContents)
End Sub

所以它被要求在特定的一行添加文本,并丢弃该行之后的所有文本?还是应该将剩余的文本进一步下移?