2016-08-26 38 views
0

我有包含我需要檢查是否存在文件的路徑下面的文本文件:VB - 逐行讀取支票存在文件中的行

我已經試着寫一些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 
+3

Side-Note:目前您只檢查文件的第一行。你也可以使用簡單的'System.IO.File.ReadAllLines'。除此之外,它還應與UNC路徑一起工作 –

+0

您是否收到錯誤? – topshot

回答

1

網絡路徑通常是這樣的:用戶後 //192.168.x.y/Users/

,你把文件的路徑。

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

我希望這會有所幫助。