代码之家  ›  专栏  ›  技术社区  ›  onlyf

VB-逐行读取文件检查是否存在

  •  0
  • onlyf  · 技术社区  · 9 年前

    我有以下文本文件,其中包含需要检查是否存在的文件路径:

    C:\path\to\file1
    C:\path\to\file2
    C:\path\to\file3
    C:\path\to\file4
    

    我已经尝试编写一些vb(第一次)来逐行读取所述文本文件,存储该行,并检查该字符串是否存在。代码如下:

    Imports System.IO
    
    Module Module1
    
    Sub Main()
    ' Store the line in this String.
    Dim line As String
    
    ' Create new StreamReader instance with Using block.
    Using reader As StreamReader = New StreamReader("file.txt")
        ' Read one line from file
        line = reader.ReadLine
    End Using
    
    ' Write if the file exists or not
    Console.WriteLine(File.Exists(line) ? "File exists." : "File does not exist.");
    End Sub
    

    我的问题是,假设我想给文件添加一个远程路径,我需要在上面的代码中更改什么才能检查它?

    C:\path\to\file1
    C:\path\to\file2
    C:\path\to\file3
    C:\path\to\file4
    \\server.domain\path\to\file5
    \\server.domain2\path\to\file6
    

    提前谢谢你。

    注释后更正:

    Imports System.IO
    
    Module Module1
    Sub Main()
    ' Loop over lines in file.
    For Each line As String In File.ReadLines("file.txt")
        ' Display the line.
        Console.WriteLine(File.Exists(line) ? "File exists." : "File does not exist.");
    Next
    End Sub
    End Module
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   NightHowler    9 年前

    网络路径通常如下: //192.168.x.y/用户/

    在Users之后,您输入了文件的路径。

    192.168.x.y是远程机器的IP地址。您也可以使用主机名代替IP地址。

    我希望这能有所帮助。

    推荐文章