Click here to Skip to main content
15,884,806 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

VB
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
Posted
Updated 17-Jul-22 23:45pm
v2
Comments
Leo Chapiro 24-Sep-18 5:47am    
It is difficult to say without to see your code, dude. Please share the appropriate code (not all you wrote) with us by adding it to "What have I tried".

1 solution

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:

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


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900