如何在 VB 中使用 Directory.GetFiles? [英] How to use Directory.GetFiles in VB?
本文介绍了如何在 VB 中使用 Directory.GetFiles?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我基本上是按照一个MS的例子来做的.下面是例子.
I basically follow a MS example.The following is the example.
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 Each f In Directory.GetFiles(d, FileName)"处有错误.我做错了什么?
I modify a little bit, so that I can use it as file search function. However there is an error at "For Each f In Directory.GetFiles(d, FileName)". What do I do wrong?
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
推荐答案
我找到了,这解决了我的问题
I find this, this solve my problem
Public Sub DirSearch(ByVal sDir As String, ByVal FileName As String)
For Each foundFile As String In My.Computer.FileSystem.GetFiles(sDir, FileIO.SearchOption.SearchAllSubDirectories, FileName)
"Do the work here"
Next
End Sub
这篇关于如何在 VB 中使用 Directory.GetFiles?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文