First of all we'll insert in colde a class. The class is:
For all extensions except pdf:Friend Class NativeMethodsPrivate Shared Function SHGetFileInfo(ByVal pszPath As String, ByVal dwFileAttributes As UInteger, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlags As UInteger) As IntPtr End Function Private Shared Function DestroyIcon(ByVal hIcon As IntPtr) As IntPtr End Function Public Structure SHFILEINFO Implements IDisposable Public hIcon As IntPtr Public iIcon As Integer Public dwAttributes As UInteger Public szDisplayName As String Public szTypeName As String Public Sub Dispose() Implements System.IDisposable.Dispose DestroyIcon(Me.hIcon) End Sub End Structure Private Const SHGFI_ICON As UInt32 = &H100 Private Const SHGFI_DISPLAYNAME As UInt32 = &H200 Private Const SHGFI_TYPENAME As UInt32 = &H400 Private Const SHGFI_LARGEICON As UInt32 = 0 Private Const SHGFI_SMALLICON As UInt32 = 1 Public Shared Function GetInfoFromShell(ByVal FileName As String) As SHFILEINFO Dim shfi As SHFILEINFO = New SHFILEINFO shfi.hIcon = IntPtr.Zero SHGetFileInfo(FileName, 0, shfi, Marshal.SizeOf(shfi), SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON) Return shfi End Function End Class
'remov' routine serves to remove duplicate files from listview Here is remov routine:Public Sub GetFiles(ByVal strFileFilter As String, ByVal strDirectory As String, ByVal intDepthLimit As Integer, ByVal intCurrentDepth As Integer) Try Dim folderInfo As New DirectoryInfo(strDirectory) ' Is the current depth on this recursion less than our limit? ' If so, find any directories and get into them by calling GetFiles recursively (incrementing depth count) If intCurrentDepth < intDepthLimit Then Dim directories() As DirectoryInfo directories = folderInfo.GetDirectories() For Each fDirectory In directories ' Recursively call ourselves incrementing the depth using the given folder path. GetFiles(strFileFilter, fDirectory.FullName, intDepthLimit, intCurrentDepth + 1) Next End If ' After we can't go further down, add any files which match our filter to listbox (in this case lstFiles) Dim files() As FileInfo files = folderInfo.GetFiles(strFileFilter) Dim sh As New NativeMethods.SHFILEINFO Dim exeIcon As System.Drawing.Icon listView1.BeginUpdate() For Each fFile In files Dim reader = My.Computer.FileSystem.ReadAllText(fFile.FullName, System.Text.Encoding.ASCII) If reader.Contains(TextBox1.Text) Then If Not fFile.FullName.EndsWith("dll") And Not fFile.FullName.EndsWith("exe") _ And Not fFile.FullName.EndsWith("res") And Not fFile.FullName.EndsWith("tx") _ And Not fFile.FullName.EndsWith("pak") And Not fFile.FullName.EndsWith("xls") And Not fFile.FullName.EndsWith("pdf") _ And Not fFile.FullName.EndsWith("pcv") Then Application.DoEvents() listView1.SmallImageList = ImageList1 listView1.LargeImageList = ImageList1 ImageList1.ColorDepth = ColorDepth.Depth32Bit sh = NativeMethods.GetInfoFromShell(fFile.FullName) exeIcon = CType(System.Drawing.Icon.FromHandle(sh.hIcon).Clone, Icon) ImageList1.ColorDepth = ColorDepth.Depth32Bit sh = NativeMethods.GetInfoFromShell(fFile.FullName) exeIcon = CType(System.Drawing.Icon.FromHandle(sh.hIcon).Clone, Icon) If (ImageList1.Images.ContainsKey(fFile.FullName)) Then listView1.Items.Add(fFile.FullName, fFile.FullName) ElseIf (Not exeIcon Is Nothing) Then ImageList1.Images.Add(fFile.FullName, exeIcon) listView1.Items.Add(fFile.FullName, fFile.FullName) Else listView1.Items.Add(fFile.FullName) End If End If End If Next listView1.EndUpdate() remov() Catch ex As Exception End Try End Sub
Private Sub remov() Try Dim i, j As Integer With Me.listView1 Do Until i > .Items.Count - 2 Dim text As String = .Items(i).Text j = i + 1 Do Until j > .Items.Count - 1 If .Items(j).Text = text Then .Items.RemoveAt(j) Else j += 1 End If Loop i += 1 Loop End With Catch End Try End SubFor pdf files the function is:
Public Sub GetFiles1(ByVal strFileFilter As String, ByVal strDirectory As String, ByVal intDepthLimit As Integer, ByVal intCurrentDepth As Integer) Try Dim folderInfo As New DirectoryInfo(strDirectory) ' Is the current depth on this recursion less than our limit? ' If so, find any directories and get into them by calling GetFiles recursively (incrementing depth count) If intCurrentDepth < intDepthLimit Then Dim directories() As DirectoryInfo directories = folderInfo.GetDirectories() For Each fDirectory In directories ' Recursively call ourselves incrementing the depth using the given folder path. GetFiles1(strFileFilter, fDirectory.FullName, intDepthLimit, intCurrentDepth + 1) Next End If ' After we can't go further down, add any files which match our filter to listbox (in this case lstFiles) Dim files() As FileInfo files = folderInfo.GetFiles(strFileFilter) For Each namo In files counter = counter + 1 Dim text As StringBuilder = New StringBuilder Dim pdfReader As PdfReader = New PdfReader(namo.FullName) Dim page As Integer = 1 Do While (page <= pdfReader.NumberOfPages) Dim strategy As ITextExtractionStrategy = New SimpleTextExtractionStrategy Dim currentText As String = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy) currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText))) text.Append(currentText) page = (page + 1) If currentText.Contains(TextBox1.Text) Then ImageList1.ColorDepth = ColorDepth.Depth32Bit Dim sh As New NativeMethods.SHFILEINFO sh = NativeMethods.GetInfoFromShell(namo.FullName) Dim theIcon As Icon If sh.hIcon <> IntPtr.Zero Then listView1.LargeImageList = ImageList1 listView1.SmallImageList = ImageList1 listView1.View = View.SmallIcon theIcon = DirectCast(System.Drawing.Icon.FromHandle(sh.hIcon).Clone, System.Drawing.Icon) ImageList1.Images.Add(namo.FullName, theIcon.Clone) listView1.Items.Add(namo.FullName, namo.FullName) End If End If Loop pdfReader.Close() Next remov() listView1.View = View.Details listView1.HeaderStyle = ColumnHeaderStyle.None listView1.FullRowSelect = True listView1.Columns.Add("", -2) Catch ex As Exception Finally End Try End Sub
To launch Getfiles and Getfiles1, examples:
GetFiles("*.ico", "C:\\myicons", 1, 0);
GetFiles1("*.pdf", "C:\\Docu", 1, 0)
References: https://www.coderslexicon.com/playing-with-recursive-directory-diving-in-vb-net
Greetings to all.
References: https://www.coderslexicon.com/playing-with-recursive-directory-diving-in-vb-net
No comments
Post a Comment