窗體上添加一個(gè)文本框,一個(gè)列表框,一個(gè)按鈕:
專(zhuān)注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)泗水免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
代碼如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyDir As String = "C:\123"
Dim MyFilter As String = "*" TextBox1.Text "*"
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyDir, MyFilter)
ListBox1.Items.Add(MyFile)
Next
End Sub
vb.net編程查找搜索指定目錄下面的所有文件和其子目錄下的文件,方法如下:
''=============================================
''名稱(chēng):?FindPath
''作用:?查找搜索指定目錄下面的所有文件和其子目錄下的文件
''參數(shù):strPath?要查找的目錄,
''strFiles?用于存查找結(jié)果的緩沖區(qū),String?類(lèi)型的動(dòng)態(tài)數(shù)組,調(diào)用時(shí)事先初始化,?如Redim?strFiles(0)
''FileCount?用于返回文件個(gè)數(shù)
''=============================================
Public?Sub?FindPath(ByVal?strPath?As?String,?strFiles()?As?String,?FileCount?As?Long)
Dim?strDirs()???As?String
Dim?strResult???As?String
Dim?FileLimit???As?Long
Dim?dirLimit????As?Long
Dim?dirCount????As?Long
Dim?I???????????As?Long
FileLimit?=?UBound(strFiles)?+?1
dirLimit?=?0
If?Right$(strPath,?1)??"/"?Then?strPath?=?strPath??"/"
strResult?=?Dir(strPath,?vbDirectory?+?vbSystem?+?vbReadOnly?+?vbHidden?+?vbNormal?+?vbArchive)
Do?While?Len(strResult)??0
If?strResult??"."?And?strResult??".."?Then
If?(GetAttr(strPath??strResult)?And?vbDirectory)??vbDirectory?Then
If?FileCount?=?FileLimit?Then
ReDim?Preserve?strFiles(FileLimit?+?10)
FileLimit?=?FileLimit?+?10
End?If
strFiles(FileCount)?=?strPath??strResult
FileCount?=?FileCount?+?1
Else
If?dirCount?=?dirLimit?Then
ReDim?Preserve?strDirs(dirLimit?+?10)
dirLimit?=?dirLimit?+?10
End?If
strDirs(dirCount)?=?strPath??strResult
dirCount?=?dirCount?+?1
End?If
End?If
strResult?=?Dir(,?vbDirectory?+?vbSystem?+?vbReadOnly?+?vbHidden?+?vbNormal?+?vbArchive)
Loop
For?I?=?0?To?dirCount?-?1
Call?FindPath(strDirs(I),?strFiles,?FileCount)
Next?I
End?Sub
思路:
1,使用ListBox1控件來(lái)存放搜索到的文件名,然后ListBox1.SelectedItem就可以返回選中項(xiàng)的文本;
2,利用獲得的選中項(xiàng)文本(即文件與路徑),使用打開(kāi)文本文件的方法,讀取內(nèi)容顯示在文本框里。
步驟如下:
窗體上添加2個(gè)列表框,一個(gè)按鈕:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyDir As String = "C:\123"
'搜索并顯示子文件夾
ListBox1.Items.Clear()
For Each MySubDir As String In System.IO.Directory.GetDirectories(MyDir)
ListBox1.Items.Add(MySubDir)
Next
'搜索并顯示文件
ListBox2.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyDir)
ListBox2.Items.Add(MyFile)
Next
End Sub
End Class
當(dāng)前文章:vb.net文件搜索 vbnet filestream
當(dāng)前URL:http://muchs.cn/article16/ddcdegg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷(xiāo)、電子商務(wù)、響應(yīng)式網(wǎng)站、ChatGPT、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)