أضع هنا كلاس يفيد في البحث عن ملف أو مجموعة ملفات في جهاز الحاسب أرجو أن يكون مفيدا
Imports System.IO Public Class FileSearch #Region " Declarations " ' المصفوفة التي ستحمل النتائج Private _FoundFiles As New ArrayList ' حدث ينطلق عند تقدم البحث Public Event FileSearchProgress(ByVal sender As Object, ByVal e As FileSearchProgressEeventArgs) ' حدث خاص للتقدم للأقراص Public Event DrivesProgress(ByVal sender As Object, ByVal e As FileSearchProgressEeventArgs) #End Region ' مشيد الفئة Sub New() ' التأكد من أن القائمة فارغة _FoundFiles.Clear() End Sub ' خاصية للقراءة فقط تعيد مصفوفة تحمل نتائج البحث Public ReadOnly Property Foundfiles() As ArrayList Get _FoundFiles.TrimToSize() Return _FoundFiles End Get End Property ' إجراء البحث عن الملفات في الأقراص ' FileName اسم الملف المراد البحث عنه ' FixedOnly للبحث في جميع الأقراص أو في الأقراص الصلبة Public Function FindFileDrives(ByVal FileName As String, Optional ByVal FixedOnly As Boolean = True) As Boolean Dim Prg As Integer = 0 _FoundFiles.Clear() For Each d As DriveInfo In DriveInfo.GetDrives If FixedOnly = True Then If d.DriveType = DriveType.Fixed Then FindFile(d.RootDirectory.ToString, FileName) End If Else FindFile(d.RootDirectory.ToString, FileName) End If Prg += 1 Dim e As New FileSearchProgressEeventArgs(DriveInfo.GetDrives().Length, Prg) RaiseEvent DrivesProgress(Me, e) Next _FoundFiles.TrimToSize() If _FoundFiles.Count > 0 Then Return True Else Return False End If End Function ' إجراء البحث عن الملفات في مجلد معين ' FileName اسم الملف المراد البحث عنه ' Foldername اسم المجلد المراد البحث فيه Public Function FindFileInFolder(ByVal FileName As String, ByVal FolderName As String) As Boolean _FoundFiles.Clear() FindFile(FolderName, FileName) _FoundFiles.TrimToSize() If _FoundFiles.Count > 0 Then Return True Else Return False End If End Function ' الإجراء الذي يقوم بعملية البحث Private Sub FindFile(ByVal dir As String, ByVal FileName As String, Optional ByVal MainFolder As Boolean = True, Optional ByVal level As Integer = 0) Dim Prg As Integer = 0 Try ' Display all files in this directory with correct indentation. For Each fname As String In Directory.GetFiles(dir, FileName) _FoundFiles.Add(fname) Next ' A recursive call for all the subdirectories in this directory For Each subdir As String In Directory.GetDirectories(dir) FindFile(subdir, FileName, False, level + 1) If MainFolder = True Then Prg += 1 Dim e As New FileSearchProgressEeventArgs(Directory.GetDirectories(dir).Length, Prg) RaiseEvent FileSearchProgress(Me, e) End If Next Catch ex As Exception ex = Nothing ' Do nothing if any error (presumably “Drive not ready”). End Try End Sub ' كلاس خاص بإطلاق الأحداث الخاصة بعملية البحث Public Class FileSearchProgressEeventArgs Inherits System.EventArgs Private m_Min As Integer = 0 Private m_Max As Integer = 100 Private m_CurPos As Integer = 0 Public ReadOnly Property Min() Get Return m_Min End Get End Property Public ReadOnly Property Max() Get Return m_Max End Get End Property Public ReadOnly Property CurPos() Get Return m_CurPos End Get End Property Public Sub New(ByVal Max As Integer, ByVal curPos As Integer, Optional ByVal Min As Integer = 0) m_Min = Min m_Max = Max m_CurPos = curPos End Sub End Class End Class
يمكن اعتبار الكود كمثال على موضوع قديم لي و هو إطلاق أحداثك الخاصة حيث أقوم هنا بإطلاق حدثين لتمرير مؤشر لعملية التقدم أو يمكنك استخدام الكلاس كما هو للقيام بعملية البحث عن ملف أو مجموعة ملفات ضمن جهاز الحاسب
انظر المثال المرفق وهو مكتوب على الـ 2005