根据我从您的问题中了解到的情况,这就是您所追求的。现在,如果有任何调整,请随时发表评论。
Private Sub Question1()
Dim list = File.ReadAllLines("yourFilePath").ToList()
Dim itemCount = list.Count
For i As Integer = 0 To itemCount - 1
If list(i) = "//This Line" AndAlso Not ((i + 1) > itemCount) Then
list(i + 1) = TextBox1.Text
End If
Next
KillProcesses(list)
End Sub
Private Sub Question2()
Dim list = TextBox1.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.None).ToList()
KillProcesses(list)
End Sub
Private Sub KillProcesses(items As List(Of String))
For Each item As String In items.Where(Function(listItem) listItem <> "//This Line") ' Exclude redundant text
Dim processes = Process.GetProcessesByName(item)
For Each process As Process In processes
Try
process.Kill()
Catch
' Do your error handling here
End Try
Next
Next
End Sub
更新:下面的代码是更新版本,以反映下面评论中要求的更改
Private Sub Question1()
Dim list = File.ReadAllLines("YourFilePath").ToList()
Dim itemCount = list.Count
For i As Integer = 0 To itemCount - 1
If list(i) = "//This Line" Then
If (i + 1 > itemCount - 1) Then ' Check if the current item is the last one in the list, if it is then add the text to the list
list.Add(TextBox1.Text)
Else ' An item exists, so just update its value
list(i + 1) = TextBox1.Text
End If
End If
Next
' Write the list back to the file
File.WriteAllLines(Application.StartupPath & "\test.txt", list)
KillProcesses(list)
End Sub