【问题标题】:check if text file contain= exists file name检查文本文件是否包含=存在文件名
【发布时间】:2023-03-05 13:01:01
【问题描述】:

你好,我有一个包含 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

【问题讨论】:

  • line 解析为每个文件名,根据您的描述的准确程度,可能会在 NewLine 或空格上拆分

标签: vb.net


【解决方案1】:

我会这样做: 首先我将文本文件下载到系统中

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

确保导入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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 2023-03-14
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    相关资源
    最近更新 更多