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


ゴン太  2008-07-01 06:36:34  No: 144891

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


オショウ  2008-07-01 09:18:49  No: 144892

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

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

以上。


魔界の仮面弁士  2008-07-01 10:37:05  No: 144893

こんな感じかな。

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 10:13:03  No: 144894

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


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加