ドライブの状態を確認し続けるには


ゴン太  2008-06-30 21:36:34  No: 144891  IP: 192.*.*.*

パソコンにUSBメモリをさしてOSにドライブとして認識されたら
所定の動作をさせたいのですが、タイマーで間隔で
System.IO.Directory.Exists で確認し続けるのが普通のやりかたでしょうか?
もっと良い方法がありましたら教えて頂きたいです。

編集 削除
オショウ  2008-07-01 00:18:49  No: 144892  IP: 192.*.*.*

http://katamari.jp/soulware/index.php/post/getting_device_notification

ここの記事が参考に・・・

以上。

編集 削除
魔界の仮面弁士  2008-07-01 01:37:05  No: 144893  IP: 192.*.*.*

こんな感じかな。


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

編集 削除
ゴン太  2008-07-02 01:13:03  No: 144894  IP: 192.*.*.*

オショウさま、魔界の仮面弁士さま、ご返信ありがとうございます。
このような便利な方法があるとは知りませんでした。
早速使わせて頂きたいと思います。
ありがとうございました。

編集 削除