- 題名: 新しく接続されたドライブの検出
- 日時: 2011/08/10 20:33:56
- ID: 28870
- この記事の返信元:
- (なし)
- この記事への返信:
- [28871] Re[1]: 新しく接続されたドライブの検出2011/08/10 21:25:58
- [29045] Re[1]: 新しく接続されたドライブの検出2011/09/14 17:44:17
- [29049] Re[1]: 新しく接続されたドライブの検出2011/09/14 22:21:31
- ツリーを表示
■No28870に返信(exefileさんの記事)
> VBで新しくドライブが接続されたのを検出したいのですが、どうすればいいのですか?
こんな感じで。
Imports System.Runtime.InteropServices
Public Class Form1
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Const WM_DEVICECHANGE As Integer = &H219
If m.Msg = WM_DEVICECHANGE Then 'デバイスの変更
Const DBT_DEVICEARRIVAL As Integer = &H8000
Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
Select Case m.WParam
Case New IntPtr(DBT_DEVICEARRIVAL)
Debug.WriteLine("追加されました。" & String.Join(", ", GetDrives(m.LParam)))
Case New IntPtr(DBT_DEVICEREMOVECOMPLETE)
Debug.WriteLine("削除されました。" & String.Join(", ", GetDrives(m.LParam)))
End Select
End If
End Sub
Private Function GetDrives(ByVal lparam As IntPtr) As String()
Dim drv As New List(Of String)()
Const DBT_DEVTYP_VOLUME As Integer = 2
If Marshal.ReadInt32(lparam, 4) = DBT_DEVTYP_VOLUME Then
'Dim dbcv_flags As DEV_BROADCAST_VOLUME_FLAGS
'dbcv_flags = DirectCast(Marshal.ReadInt32(lparam, 16), DEV_BROADCAST_VOLUME_FLAGS)
Dim dbcv_unitmask As Integer = Marshal.ReadInt32(lparam, 12)
Dim mask As Integer = dbcv_unitmask
For c As Integer = Asc("A") To Asc("Z")
If (mask And 1) = 1 Then
drv.Add(Chr(c) & ":")
End If
mask \= 2
Next
End If
Return drv.ToArray()
End Function
'<Flags()> Private Enum DEV_BROADCAST_VOLUME_FLAGS
' DBTF_MEDIA = 1
' DBTF_NET = 2
'End Enum
End Class
分類:[.NET]
VBで新しくドライブが接続されたのを検出したいのですが、どうすればいいのですか?