如何使用VB.NET检测窗口7上的USB设备? [英] How to detect usb device on window 7 using VB.NET ?

查看:128
本文介绍了如何使用VB.NET检测窗口7上的USB设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的电脑上安装了win.10并且我已经使用Win32_PnPEntity类在2017年的vb.net中进行了应用程序检测计算机部件,USB和MOBILE以及它在win10中的工作非常好。但是当我在win.7上运行这个应用程序时,它没有检测到任何计算机部件,USB和MOBILE。



我尝试了什么:



I have installed win.10 in my PC and I have made application in vb.net 2017 using Win32_PnPEntity class that detect computer parts, USB and MOBILE and its working very nice in win10. but when i am running this application on win.7 its not detecting any computer parts, USB and MOBILE.

What I have tried:

Imports System.Management


Public Class Form1

Private Sub Get_Dtl(ByVal Val As String)
Try
Dim path As ManagementPath = New ManagementPath()
path.Server = "."
path.NamespacePath = "root\CIMV2"
Dim scope As ManagementScope = New ManagementScope(path)
Dim query As ObjectQuery = New ObjectQuery("SELECT * FROM Win32_PnPEntity")
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(scope, query)
Dim queryCollection As ManagementObjectCollection = searcher.Get()
Dim A As Integer = 0

For Each m In queryCollection
If Val = "USB" AndAlso m("Service") = "WUDFWpdFs" Then
A += 1
DataGridView1.Rows.Add(A, m("Caption"), m("Description"), m("DeviceID"), m("HardwareID"), m("Manufacturer"), m("Name"), m("PNPClass"), m("PNPDeviceID"), m("Service"), m("Status"))

ElseIf Val = "Mobile" AndAlso m("Service") = "WUDFWpdMtp" Then
A += 1
DataGridView1.Rows.Add(A, m("Caption"), m("Description"), m("DeviceID"), m("HardwareID"), m("Manufacturer"), m("Name"), m("PNPClass"), m("PNPDeviceID"), m("Service"), m("Status"))

ElseIf Val = "All" Then
A += 1
DataGridView1.Rows.Add(A, m("Caption"), m("Description"), m("DeviceID"), m("HardwareID"), m("Manufacturer"), m("Name"), m("PNPClass"), m("PNPDeviceID"), m("Service"), m("Status"))
End If
Next
If A = 0 Then
MsgBox("No Record Found")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub Btn_All_Click(sender As Object, e As EventArgs) Handles Btn_All.Click
DataGridView1.Rows.Clear()
Get_Dtl("All")
End Sub

Private Sub BTN_Mobile_Click(sender As Object, e As EventArgs) Handles BTN_Mobile.Click
DataGridView1.Rows.Clear()
Get_Dtl("Mobile")
End Sub

Private Sub Btn_USB_Click(sender As Object, e As EventArgs) Handles Btn_USB.Click
DataGridView1.Rows.Clear()
Get_Dtl("USB")
End Sub

End Class

推荐答案

您可能想要查看 DriveInfo.GetDrives方法(System.IO) | Microsoft Docs [ ^ ]。



MS的好朋友已经为我们提供了很好的帮助。以下是使用此方法的方法:



You might want to take a look at DriveInfo.GetDrives Method (System.IO) | Microsoft Docs[^].

Nice folks at MS have been kind enough to wrap it up for us. Here is how you can use this method:

var drives = DriveInfo.GetDrives();
		foreach (var drive in drives)
		{
			if (drive.IsReady && drive.DriveType == DriveType.Removable)
			{
				Console.WriteLine(drive.Name);
				Console.WriteLine(drive.DriveFormat);
			}
		}





我无法将其作为.Net Fiddle运行(我用来检查网站的网站)否认权限。如果你有必要的权限集,它应该可以工作。



I could not run this as .Net Fiddle (website I use to check snippets) denies the permissions. It should work if you have required set of permissions though.


这篇关于如何使用VB.NET检测窗口7上的USB设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆