检查文本文件是否包含=存在文件名

时间:2014-05-07 17:56:12

标签: vb.net

你好,我有一个文本文件包含3行filename1 filename2 filename3我想检查是否 其中一个=存在文件名我的代码只有在我的文本文件只包含1行

时才能工作
        Dim address As String = ("http://localhost/file.txt")
    Dim client As WebClient = New WebClient()
    Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
    Dim line As String = reader.ReadToEnd
    If File.Exists(line) = True Then
        MsgBox("FOUND")
    End If

1 个答案:

答案 0 :(得分:0)

我就是这样做的: 首先,我将文本文件下载到系统

然后我读取文本文件的每一行并检查文件是否存在:

确保导入system.io

Imports System.IO

添加以下功能:

 Public Function ReadALine(ByVal File_Path As String, ByVal Line2Read As Integer) As String
    Dim Buffer As Array
    Dim Line As String
    Buffer = File.ReadAllLines(File_Path)
    Line = Buffer(Line2Read)
    Return Line
End Function

现在假设您已将文本文件下载到C:目录

Dim filename As String = "C:\file.txt"
    If My.Computer.FileSystem.FileExists(ReadALine(filename, 0)) Then
        MsgBox("FOUND")
    End If

将数字0更改为1或更改为2以获取第二行和第三行

Read a text file line by line - VB.NET