パソコンにUSBメモリをさしてOSにドライブとして認識されたら
所定の動作をさせたいのですが、タイマーで間隔で
System.IO.Directory.Exists で確認し続けるのが普通のやりかたでしょうか?
もっと良い方法がありましたら教えて頂きたいです。
http://katamari.jp/soulware/index.php/post/getting_device_notification
ここの記事が参考に・・・
以上。
こんな感じかな。
Imports System.Runtime.InteropServices
Public Class Form1
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = &H219 Then
If m.WParam = New IntPtr(&H8000) Then
Dim drives() As String = GetDrives(m.LParam)
If drives.Length > 0 Then
ListBox1.Items.Add(Join(drives) & "が追加されました。")
End If
ElseIf m.WParam = New IntPtr(&H8004) Then
Dim drives() As String = GetDrives(m.LParam)
If drives.Length > 0 Then
ListBox1.Items.Add(Join(drives) & "が削除されました。")
End If
End If
End If
MyBase.WndProc(m)
End Sub
Private Shared Function GetDrives(ByVal p As IntPtr) As String()
If Marshal.ReadInt32(p, 4) <> &H2 OrElse Marshal.ReadInt16(p, 16) = &H1 Then
Return New String() {}
End If
Dim list As New List(Of String)()
Dim unitmask As Integer = Marshal.ReadInt32(p, 12)
For i As Integer = 0 To 25
If (unitmask And 1) = 1 Then
list.Add(Chr(i + Asc("A")) & ":")
End If
unitmask >>= 1
Next
Return list.ToArray()
End Function
End Class
オショウさま、魔界の仮面弁士さま、ご返信ありがとうございます。
このような便利な方法があるとは知りませんでした。
早速使わせて頂きたいと思います。
ありがとうございました。