VB - 如果 StreamReader 读取包含 ("string1") 但缺少 ("string2") 的行

VB - If StreamReader reads line containing ("string1") but missing ("string2")

编辑: 我将如何检索满足上述条件的文件的文件名?

如何检查字符串是否包含 "value1" 但不包含 "value2"?我试过这个:

While Not sr.EndOfStream
    Dim sLine As String = sr.ReadLine
    If sLine.Contains("value1") Then
        If Not sLine.Contains("value2") Then
            sw.WriteLine("write name of file that meets conditions to txt file")
End While

不确定如何搜索缺失值。

If sLine.Contains("value1") = True and sLine.Contains("value2") = False Then
    Msgbox("sLine contains value1, but does not contain value2")
End If

您可以将整个文件加载到一个字符串中并进行检查

If wholeFileData.Contains("value1") AndAlso Not wholeFileData.Contains("value2") Then
    sw.WriteLine("write name of file that meets conditions to txt file")
End If

如果您需要循环每一行,那么您需要存储在一个变量中并在末尾显示消息。

Dim containsValue1, containsValue2 As Boolean

containsValue1 = False
containsValue2 = False

While Not sr.EndOfStream
    Dim sLine As String = sr.ReadLine

    If sLine.Contains("value1") Then
        containsValue1 = True
    End If

    If sLine.Contains("value2") Then
        containsValue2 = True
    End If
End While

If containsValue1 AndAlso Not containsValue2 Then
    sw.WriteLine("write name of file that meets conditions to txt file")
End If