2011-02-06 3 views
1

대신 FileSystemWachter 클래스 대신 새로운 드라이브 문자 팝업이 나타날 때와 비슷한 것을 찾고 있습니다. 예를 들어 USB 디스크가 연결되어 있거나 SD 카드가 삽입되어있는 경우 새로운 드라이브 문자가 나타납니다. 이 일이 발생하면 내 앱에서 이벤트를하고 싶습니다.드라이브 문자 감시자

FileSystemWatcher 클래스를이 클래스에 사용할 수 있습니까? 아니면 이에 관련된 것이 있습니까?

예 또는 제안 사항이 있습니까?

+0

얼마나 빨리 앱에 응답 하시겠습니까? –

+0

가능한 한 빨리 : – Marcel

답변

5

이 시도 : http://www.dotnetthoughts.net/2009/02/13/how-to-detect-usb-insertion-and-removal-in-vbnet/

Private WM_DEVICECHANGE As Integer = &H219 

Public Enum WM_DEVICECHANGE_WPPARAMS As Integer 
    DBT_CONFIGCHANGECANCELED = &H19 
    DBT_CONFIGCHANGED = &H18 
    DBT_CUSTOMEVENT = &H8006 
    DBT_DEVICEARRIVAL = &H8000 
    DBT_DEVICEQUERYREMOVE = &H8001 
    DBT_DEVICEQUERYREMOVEFAILED = &H8002 
    DBT_DEVICEREMOVECOMPLETE = &H8004 
    DBT_DEVICEREMOVEPENDING = &H8003 
    DBT_DEVICETYPESPECIFIC = &H8005 
    DBT_DEVNODES_CHANGED = &H7 
    DBT_QUERYCHANGECONFIG = &H17 
    DBT_USERDEFINED = &HFFFF 
End Enum 

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
    If m.Msg = WM_DEVICECHANGE Then 
     Select Case m.WParam 
      Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL 
       lblMessage.Text = "USB Inserted" 
      Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE 
       lblMessage.Text = "USB Removed" 
     End Select 
    End If 
    MyBase.WndProc(m) 
End Sub 
+0

나는 이것을 보았지만 어떤 드라이브 문자가 추가되었는지 알지 못합니다. 이제 USB 장치가 삽입되었습니다. – Marcel

+0

WM_DEVICECHANGE가 DBT_DEVICEARRIVAL 타입 일 때, 디바이스의 정보를 얻는 struct를 포함해야하는 lParam 객체도 포함합니다. DEV_BROADCAST_HDR 구조체에 있습니다. VB에서 이것을 랩핑하면 디바이스에 ID를 부여 할 수 있습니다. http://msdn.microsoft.com/en-us/library/aa363246(v=vs.85).aspx –

0

하나의 솔루션은 WMI 될 수 있습니다 특히

Computer System Hardware Classes

+0

WMI의 단점은 변경 사항을 확인하기 위해 계속해서 쿼리해야한다는 것입니다. 또한 WMI가 그렇게 빠르지 않습니다. – alex

0

여기 타이머를 사용하여 솔루션입니다. 이것은 USB 유형 장치에만 국한되지 않습니다.

Dim WithEvents myTimer As New Timers.Timer 
Private Sub Form1_Shown(ByVal sender As Object, _ 
         ByVal e As System.EventArgs) Handles Me.Shown 

    'start a timer to watch for new drives 
    myTimer.Interval = 1000 
    myTimer.AutoReset = True 
    myTimer.Start() 
    drvs.AddRange(IO.Directory.GetLogicalDrives) 'get initial set of drives 

End Sub 

Dim drvs As New List(Of String) 
Private Sub myTimer_Elapsed(ByVal sender As Object, _ 
          ByVal e As System.Timers.ElapsedEventArgs) Handles myTimer.Elapsed 

    Dim cDrvs As New List(Of String)(IO.Directory.GetLogicalDrives) 'get current drives 
    Dim eDrvs As IEnumerable(Of String) 
    Dim add_or_remove As Integer = 0 '0 = same number, 1 = removed, 2 = add 

    If cDrvs.Count = drvs.Count Then 
     'same number of drives - check that they are the same 
     eDrvs = drvs.Except(cDrvs) 
    ElseIf cDrvs.Count < drvs.Count Then 
     'drive(s) removed 
     eDrvs = drvs.Except(cDrvs) 
     add_or_remove = 1 
     Debug.WriteLine("R") 
    ElseIf cDrvs.Count > drvs.Count Then 
     'new drive(s) 
     eDrvs = cDrvs.Except(drvs) 
     add_or_remove = 2 
     Debug.WriteLine("A") 
    End If 

    For Each d As String In eDrvs 
     Debug.WriteLine(d) 

    Next 
    drvs = cDrvs 'set list of current drives 
End Sub 
+0

고마워,이 일자리 점! – Marcel

관련 문제