如果该行包含一些字符串,则从文本文件中删除一行

Jim*_*Del 3 string vb6 text-files

在VB6中,我正在寻找一种方法来从文本文件中删除一行文本,如果该行包含一些字符串.我主要用C#工作,我在这里不知所措.使用.NET有几种方法可以做到这一点,但我很幸运,他必须维护一些旧的VB代码.

有没有办法做到这一点?

谢谢

C-P*_*uru 5

假设您在变量中有文件名sFileName:

Dim iFile as Integer
Dim sLine as String, sNewText as string

iFile = FreeFile

Open sFileName For Input As #iFile
Do While Not EOF(iFile)
  Line Input #iFile, sLine
  If sLine Like "*foo*" Then
    ' skip the line
  Else
    sNewText = sNewText & sLine & vbCrLf
  End If
Loop
Close

iFile = FreeFile
Open sFileName For Output As #iFile
Print #iFile, sNewText
Close
Run Code Online (Sandbox Code Playgroud)

您可能希望输出到不同的文件而不是覆盖源文件,但希望这会让您更接近.