3
我基本上遵循MS示例。以下是示例。如何在VB中使用Directory.GetFiles?
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
Try
' Only get files that begin with the letter "c."
Dim dirs As String() = Directory.GetFiles("c:\", "c*")
Console.WriteLine("The number of files starting with c is {0}.", dirs.Length)
Dim dir As String
For Each dir In dirs
Console.WriteLine(dir)
Next
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
我修改了一下,以便我可以使用它作爲文件搜索功能。但是,在「For Directory Directory.GetFiles(d,FileName)」中有錯誤。我做錯了什麼?
Public Sub DirSearch(ByVal sDir As String, ByVal FileName As String)
Dim d As String
Dim f As String
Try
For Each d In Directory.GetDirectories(sDir)
For Each f In Directory.GetFiles(d, FileName)
If f = FileName Then
Form1.TextBox4.Text = "1"
Else
Form1.TextBox4.Text = "0"
End If
Next
DirSearch(d, FileName)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Message)
End Try
End Sub
究竟是你得到的錯誤? – Thousand
這段代碼在我的機器上運行正常;不過,我不認爲你的邏輯是正確的。檢查f = FileName的意圖不明確。 「f」將是完整路徑,「FileName」是不包含路徑的文件名或搜索表達式;我不認爲IF條件會得到滿足。 –
@ JimO'Neil你說得對。我的程序中有邏輯錯誤 – Marco