Für solche Zwecke kann man z.B. die Windows-API verwenden:
(Windows.Forms-Formular)
Imports System
Imports System.IO
Imports System.Runtime
Imports System.Runtime.InteropServices
Public Class frmEnumerateFiles
'Code übernommen und leicht modifiziert von ....
'https://www.vbarchiv.net/forum/read.php?f=10&t=59926&i=59957
Private Declare Auto Function FindFirstFile Lib "kernel32.dll" (ByVal _
lpFileName As String, ByRef lpFindData As WIN32_FIND_DATA) As IntPtr
Private Declare Auto Function FindNextFile Lib "kernel32.dll" (ByVal _
hFindFile As IntPtr, ByRef lpFindData As WIN32_FIND_DATA) As IntPtr
Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As _
IntPtr) As Boolean
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
Private Structure WIN32_FIND_DATA
Public sfileAttributes As Int32
Public creationTime_lowDateTime As Int32
Public creationTime_highDateTime As Int32
Public lastAccessTime_lowDateTime As Int32
Public lastAccessTime_highDateTime As Int32
Public lastWriteTime_lowDateTime As Int32
Public lastWriteTime_highDateTime As Int32
Public nFileSizeHigh As Int32
Public nFileSizeLow As Int32
Public dwReserved0 As Int32
Public dwReserved1 As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)>
Public fileName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)>
Public alternateFileName As String
End Structure
Private Const MAX_PATH As Integer = 260
Private Const INVALID_HANDLE_VALUE As Integer = -1
Private Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Dim files As New List(Of String)
FindAllFiles("C:\users", "*.pdf", files)
Stop
End Sub
Private Sub FindAllFiles (ByVal folder As String, ByVal pattern As String,
ByRef files As List(Of String))
Dim w32data As New WIN32_FIND_DATA
Dim Handle As System.IntPtr = FindFirstFile(Path.Combine(folder, _
"*.*"), w32data)
If Handle <> CType(INVALID_HANDLE_VALUE, System.IntPtr) Then
Dim Search As Boolean = True
Do
'Handelt es sich um ein Verzeichnis?
If (w32data.sfileAttributes And FILE_ATTRIBUTE_DIRECTORY) =
FILE_ATTRIBUTE_DIRECTORY Then
'Verzeichnisnamen ermitteln und für den späteren rekursiven
' Aufruf speichern
If w32data.fileName <> "." And w32data.fileName <> ".." Then
FindAllFiles(Path.Combine(folder, w32data.fileName),
pattern, files)
End If
Else
'es ist eine datei
If w32data.fileName Like pattern Then
files.Add(Path.Combine(folder, w32data.fileName))
End If
End If
Loop Until FindNextFile(Handle, w32data) = CType(False, _
System.IntPtr)
'handle schliessen
FindClose(Handle)
End If
End Sub
End Class |