تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
تعقب إضافة وإزالة الأقراص المرتبطة عبر منفذ Usb
#3


وفيمايلي نص الفئة الكامل

كود :
Public Class UsbDriveDetect

Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher

Public Enum EnUsbArrivedRemoved
Arrived
Removed
End Enum

Public Event UsbDeviceArrivedRemoved(ByVal sender As Object, ByVal e As UsbDriveEeventArgs)

Public Structure DriveInfoStr
Dim DriveLetter As String
Dim Description As String
Dim FileSystem As String
Dim Size As UInt64
Dim FreeSpace As UInt64
Dim DriveType As DriveTypeEnum
Dim VolumeName As String
Dim VolumeSerialNumber As String
End Structure

Public Enum DriveTypeEnum
Unknown = 0
NoRootDirectory = 1
RemovableDisk = 2
LocalDisk = 3
NetworkDrive = 4
CompactDisc = 5
RAMDisk = 6
End Enum

Sub New()
Dim Query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " _
& "WHERE TargetInstance ISA 'Win32_DiskDrive'")
Me.m_MediaConnectWatcher = New ManagementEventWatcher
Me.m_MediaConnectWatcher.Query = Query2
End Sub

Public Sub StartDetection()
Me.m_MediaConnectWatcher.Start()
End Sub

Public Sub StopDetection()
Me.m_MediaConnectWatcher.Stop()
End Sub

Public Class UsbDriveEeventArgs
Inherits System.EventArgs

Private m_DeviceName As String
Private m_DriveLetter As String
Private m_ArrivedRemoved As EnUsbArrivedRemoved
Private m_DriveInfoS As DriveInfoStr

Sub New(ByVal DeviceName As String, ByVal DriveLetter As String, ByVal ArrivedRemoved As EnUsbArrivedRemoved _
, ByVal DriveInformations As DriveInfoStr)
Me.m_DeviceName = DeviceName
Me.m_DriveLetter = DriveLetter
Me.m_ArrivedRemoved = ArrivedRemoved
Me.m_DriveInfoS = DriveInformations
End Sub

Public ReadOnly Property DriveInformation() As DriveInfoStr
Get
Return m_DriveInfoS
End Get
End Property

Public ReadOnly Property DeviceName() As String
Get
Return Me.m_DeviceName
End Get
End Property

Public ReadOnly Property DriveLetter() As String
Get
Return Me.m_DriveLetter
End Get
End Property

Public ReadOnly Property ArrivedRemoved() As EnUsbArrivedRemoved
Get
Return Me.m_ArrivedRemoved
End Get
End Property

End Class

Private Sub m_MediaConnectWatcher_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived

Dim mbo, obj As ManagementBaseObject
' the first thing we have to do is figure out if this is a creation or deletion event
mbo = CType(e.NewEvent, ManagementBaseObject)
' next we need a copy of the instance that was either created or deleted
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
If obj("InterfaceType") = "USB" Then
Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
Dim DrL As String = GetDriveLetterFromDisk(obj("Name"))
Dim Ee As New UsbDriveEeventArgs(obj("Caption"), DrL, EnUsbArrivedRemoved.Arrived, GetDriveInformation(DrL))
RaiseEvent UsbDeviceArrivedRemoved(Me, Ee)
Case "__InstanceDeletionEvent"
Dim Ee As New UsbDriveEeventArgs(obj("Caption"), Nothing, EnUsbArrivedRemoved.Removed, Nothing)
RaiseEvent UsbDeviceArrivedRemoved(Me, Ee)
End Select
End If
End Sub

Private Function GetDriveLetterFromDisk(ByVal Name As String) As String
Dim oq_part, oq_disk As ObjectQuery
Dim mos_part, mos_disk As ManagementObjectSearcher
Dim obj_part, obj_disk As ManagementObject
Dim ans As String = ""

' WMI queries use the "\" as an escape charcter
Name = Replace(Name, "\", "\\")

' First we map the Win32_DiskDrive instance with the association called
' Win32_DiskDriveToDiskPartition. Then we map the Win23_DiskPartion
' instance with the assocation called Win32_LogicalDiskToPartition

oq_part = New ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & Name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
mos_part = New ManagementObjectSearcher(oq_part)
For Each obj_part In mos_part.Get()

oq_disk = New ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & obj_part("DeviceID") & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk("Name") & ","
Next
Next

Return ans.Trim(","c)

End Function

Private Function GetDriveInformation(ByVal DriveLetter As String) As DriveInfoStr
Dim Query As String = "Select * from Win32_LogicalDisk WHERE DeviceID = '" & DriveLetter & "'"
Dim colDisks As New ManagementObjectSearcher(Query)
For Each objDisk As ManagementObject In colDisks.Get
Dim DrIn As DriveInfoStr
With DrIn
.DriveLetter = objDisk("DeviceID")
.FileSystem = objDisk("FileSystem")
.Size = objDisk("Size")
.FreeSpace = objDisk("FreeSpace")
.Description = objDisk("Description")
.DriveType = objDisk("DriveType")
.VolumeName = objDisk("VolumeName")
.VolumeSerialNumber = objDisk("VolumeSerialNumber")
End With
Return DrIn
Next
Return Nothing
End Function

End Class



وفيمايلي مثال عن الاستخدام

عرف متغيرا عاما يشير إلى فئتنا ضمن فئة النموذج

كود :
Private WithEvents UsbMonitor As New UsbDriveDetect
أضف زري أوامر لبدء وإيقاف التعقب

كود :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.UsbMonitor.StartDetection()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.UsbMonitor.StopDetection()
End Sub
عالج الحدث الصادر عن فئتنا إظهار رسالة مثلا

كود :
Private Sub UsbMonitor_UsbDeviceArrivedRemoved(ByVal sender As Object, ByVal e As UsbDriveDetect.UsbDriveEeventArgs) _
Handles UsbMonitor.UsbDeviceArrivedRemoved

MsgBox(e.DeviceName & ControlChars.CrLf & e.DriveLetter & ControlChars.CrLf & e.ArrivedRemoved.ToString)
End Sub
}}}
تم الشكر بواسطة:


الردود في هذا الموضوع
تعقب إضافة وإزالة الأقراص المرتبطة عبر منفذ Usb - بواسطة Raggi Tech - 03-10-12, 09:25 AM

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  إضافة و نسخ الخطوط الخاصّة لمجلّد خطوط الوينداوز عبد العزيز البسكري 2 4,198 08-05-23, 12:03 AM
آخر رد: ابو محمد محمد محمد
  [مقال] إضافة تقارير Crystal report إلى فيجوال 2010 اسامه الهرماوي 3 6,106 05-10-21, 01:38 PM
آخر رد: mohameddahab867
  [مقال] إضافة خاصية التحجيم التلقائي - AutoSize - إلى أداة مربع النص - TextBox sooriaty03 10 12,812 21-03-21, 09:53 PM
آخر رد: عبد الهادي بهاب
  مثال للتعامل مع الجداول المرتبطة - عن طريق الكلاسات ابو ليلى 24 16,439 04-10-20, 06:16 PM
آخر رد: محمد بن عطية
  طريقة إضافة اختصار للصنف في شاشة المبيعات ملهمـ 5 6,420 14-07-20, 12:24 PM
آخر رد: ابراهيم ايبو
  إضافة نغمات إفتتاحيّة أثناء تسطيب البرنامج عبد العزيز البسكري 11 7,888 23-01-19, 08:35 PM
آخر رد: عبد العزيز البسكري
  الكامل في VB.Net (التعامل مع الأقراص و المجلدات و الملفات) rinawi 3 4,367 25-05-13, 01:11 PM
آخر رد: shaker.soft
  الإصدار الاول من إضافة الرسائل ( Msge ) والصور مجانآ ali.alfoly 12 6,588 25-05-13, 01:10 PM
آخر رد: shaker.soft
  تعلم إضافة ألعاب الفلاش و مقاطع الفيديو الفلاشية إلى برنامجك Aly El-Haddad 1 3,129 13-04-13, 06:46 PM
آخر رد: Sajad
  عمل ملف Setup لتنصيب برنامجك + إضافة برامج أساسية ظمن عملية التنصيب(شرح بالصور) RaggiTech 0 7,522 05-10-12, 02:25 AM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم