Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 驱动器号监视程序_C#_.net_Vb.net - Fatal编程技术网

C# 驱动器号监视程序

C# 驱动器号监视程序,c#,.net,vb.net,C#,.net,Vb.net,我现在看到的不是FileSystemWachter类,而是一个类似于弹出新驱动器号时的外观。例如,当连接usb磁盘或插入SD卡等时,您将获得一个新的驱动器号。发生这种情况时,我希望在我的应用程序中有一个事件 您可以为此使用FileSystemWatcher类吗,或者是否有特定的内容 有什么例子或建议吗 试试这个: 一种解决方案可以是WMI,尤其是 下面是一个使用计时器的解决方案。这并不特定于USB类型的设备 Dim WithEvents myTimer As New Timers.Timer P

我现在看到的不是FileSystemWachter类,而是一个类似于弹出新驱动器号时的外观。例如,当连接usb磁盘或插入SD卡等时,您将获得一个新的驱动器号。发生这种情况时,我希望在我的应用程序中有一个事件

您可以为此使用FileSystemWatcher类吗,或者是否有特定的内容

有什么例子或建议吗

试试这个:


一种解决方案可以是WMI,尤其是


下面是一个使用计时器的解决方案。这并不特定于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
Dim以事件myTimer作为新计时器。计时器
显示私有子表单1_(ByVal发送方作为对象_
ByVal e As System.EventArgs)处理我。如图所示
'启动计时器以监视新驱动器
myTimer.Interval=1000
myTimer.AutoReset=True
myTimer.Start()
drvs.AddRange(IO.Directory.getLogicalDrive)“获取驱动器的初始集
端接头
Dim DRV作为新列表(字符串)
私有子myTimer_已过(ByVal发送方作为对象_
ByVal e作为System.Timers.ElapsedEventArgs)处理myTimer
将CDRV作为新列表(字符串)(IO.Directory.getLogicalDrive)“获取当前驱动器”
Dim EDRV作为IEnumerable(字符串)
Dim add_或_remove As Integer=0'0=相同的数字,1=删除,2=添加
如果cDrvs.Count=drvs.Count,则
'相同数量的驱动器-检查它们是否相同
EDRV=除CDRV外的drvs
ElseIf cDrvs.Countdrvs.Count
“新驱动器”
eDrvs=cDrvs.除外(drvs)
添加或删除=2
Debug.WriteLine(“A”)
如果结束
对于EDRV中的每个d As字符串
Debug.WriteLine(d)
下一个
drvs=cDrvs设置当前驱动器列表
端接头

您希望应用程序以多快的速度响应它?我看到了这一点,但我不知道添加了哪个驱动器号。我现在才知道插入了一个USB设备。当WM_DEVICECHANGE的类型为DBT_DEVICEARRIVAL时,它还将包含一个lParam对象,该对象应包含一个获取设备信息的结构。它将在结构开发广播HDR中。如果你用VB把它包装起来,你就可以识别设备了。WMI的缺点是,您必须不断地查询它以检查更改;另外,WMI也没那么快。谢谢,这会帮你完成任务的!
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