反复搜索目录以查找文件扩展名 [英] Searching Directory Recrusivly For File Extensions
问题描述
我在网上浏览了60多个页面,以寻找最佳方法来搜索目录和子目录中的指定文件扩展名.我已构建了防病毒软件,但不希望它扫描每种文件类型.
我在哪里可以更改此代码,以仅扫描多个文件扩展名?而不是每个文件?
示例:
.exe
.com
.sys
等等.
这段代码是我当前正在使用的递归形式.
I have looked through more than 60 pages on the net to try and find the best method to search directories and sub-directories for specified file extensions. I have built an anti-virus but do not wish for it to scan every file type.
Where can I alter this code to allow multiple file extensions to be scanned only? and not every file?
Example:
.exe
.com
.sys
and so forth.
This code is the form of recursion that I am currently using.
Public Sub starting_scan()
stop_Scan = False
If Directory.Exists(myscanpath.Text) = True Then
Scanning = True
ScanScanner(txtScanningNow.Text)
Else
Timer1.Stop()
MsgBox("The Selected Drive Or Device Is Not Ready Or Has No Scannable Content", MsgBoxStyle.Critical)
SwitchButton4.Value = False
Exit Sub
End If
Dim drives As String() = Directory.GetLogicalDrives()
Dim aDrive As String
For Each aDrive In drives
myscanpath.Text = aDrive
ScanScanner(txtScanningNow.Text)
Next
stop_Scan = True
Timer1.Stop()
Scanning = False
txtScanningNow.Text = comboBox1.SelectedItem
SwitchButton4.Value = False
If lblvirus.Text = 0 Then
DelVirusInfection.Enabled = False
Else
DelVirusInfection.Enabled = True
End If
End Sub
Sub ScanScanner(ByVal dir As String)
MD5Text.Text = MD5Reader.ReadToEnd
Try
With My.Computer.FileSystem
If stop_Scan = True Then
Exit Sub
End If
For Each file1 In Directory.EnumerateFiles(dir)
Dim fs As New FileInfo(file1)
ComputerFileHash.Text = getMd5Hash(fs.FullName)
labelX13.Text = getSHA1Hash(fs.FullName)
If MD5Text.Text.Contains(ComputerFileHash.Text) Then
CheckedListBoxControl1.Items.Add(fs.FullName)
lblvirus.Text = CheckedListBoxControl1.Items.Count
End If
getmyfileatr(file1)
Dim fileDetail As IO.FileInfo
fileDetail = My.Computer.FileSystem.GetFileInfo(file1)
lblSizef.Text = fileDetail.Length
txtScanningNow.Text = file1
Next file1
' Search child directories.
For Each folder As Object In .GetDirectories(dir)
txtScanningNow.Text = folder
ScanScanner(folder)
Next folder
End With
Catch ex As UnauthorizedAccessException
Return
End Try
End Sub
我该如何过滤?
预先谢谢您.
How do I filter this?
Thank you in advance.
推荐答案
您没有在上述条件中给出搜索条件.
检查以下代码以在目录和子目录中搜索
You have not given a search criteria in above criteria.
check the following code to search in directory and sub directories
dim filePaths() as string = System.IO.Directory.GetFiles(@"c:\MyDir\", "*.bmp",SearchOption.AllDirectories)
首先,Prasad的解决方案可以如果没有一个疯狂的问题在这里得到解决,请给出正确的解决方案:目录. Get.Files搜索模式问题 [ ^ ].
First of all, the solution by Prasad could be correct if not one crazy problem explained here, with the solution: Directory.Get.Files search pattern problem[^].
据我了解,GetFiles
方法仅返回一个文件扩展名.我在问如何通过更改到目前为止的代码来搜索多个文件扩展名.
As I understand the GetFiles
method only returns one file extension. I am asking how to search for multiple file extensions by altering the code i have so far.
很难猜到吗?您需要为每个扩展名做相同的事情,一个接一个地合并从每次调用GetFiles
获得的结果.
Is it hard to guess? You need to do the same thing for each extension, one-by-one and unite results obtained from each call to GetFiles
.
这篇关于反复搜索目录以查找文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!