VB.NET - Funzione che Recupera Serial Number dei Dispositivi USB Collegati al Computer (Codice)
Oggi vi voglio mettere a disposizione una Funzione che Recupera il Serial Number dei dispositivi USB collegati al computer e le visualizza in una DataGridView o Tabella .
L'algoritmo sfrutta l'interrogazioni WMI . Per creare le giuste interrogazioni (Query) al Sistema Operativo Microsoft basta scaricare il seguente programma all'indirizzo Download WMICodeCreator.zip
Prima di procedere dovete Aggiungere nei Riferimenti la Classe System.Management facendo : Progetto >> Aggiungi Riferimento >> System.Management
CODICE VISUAL STUDIO 2010 - VISUAL BASIC .NET - VB.NET:
Imports System.Management
Imports System.Text.RegularExpressions
Public Class Form1
Dim DeviceID As String = ""
Private Sub GetSerialUSBDeviceID()
Dim moReturn As Management.ManagementObjectCollection
Dim moSearch As Management.ManagementObjectSearcher
Dim mo As Management.ManagementObject
moSearch = New Management.ManagementObjectSearcher("Select * from Win32_USBControllerDevice")
moReturn = moSearch.Get
For Each mo In moReturn
Dim moReturnDevice As Management.ManagementObjectCollection
Dim moSearchDevice As Management.ManagementObjectSearcher
Dim moDevice As Management.ManagementObject
Dim strDeviceName As String = mo("Dependent").ToString.Replace(""""c, "")
Dim strDevice As String = strDeviceName.Substring(strDeviceName.IndexOf("=") + 1)
moSearchDevice = New Management.ManagementObjectSearcher("Select * From Win32_PnPEntity Where DeviceID = '" & strDevice & "'")
moReturnDevice = moSearchDevice.Get
For Each moDevice In moReturnDevice
Dim strDeviceID As String
strDeviceID = moDevice("DeviceID")
strDeviceID.ToUpper()
' MessageBox.Show("DeviceID is " + strDeviceID)
' MessageBox.Show(String.Format("{0} - {1} - {2}", moDevice("Description").ToString, moDevice("Status").ToString, moDevice("Name").ToString))
DeviceID = Convert.ToString(moDevice("PNPDeviceID").ToString)
Dim ArrUSBInfo() As String = DeviceID.Split("\")
' MessageBox.Show(String.Format("Name : {0} - SerialNamber : {1}", moDevice("Name").ToString, ArrUSBInfo(UBound(ArrUSBInfo))))
DataGridView1.Rows.Add(moDevice("Name").ToString, moDevice("PNPDeviceID").ToString, ArrUSBInfo(UBound(ArrUSBInfo)).ToString)
Next
Next
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("NomeDevice", "Nome Device")
DataGridView1.Columns.Add("DeviceID", "PNPDeviceID")
DataGridView1.Columns.Add("SerialNumber", "Serial Number")
DataGridView1.AllowUserToAddRows = False
Dim FontBold As New Font(DataGridView1.DefaultCellStyle.Font.FontFamily, 11, FontStyle.Bold)
DataGridView1.Columns("NomeDevice").DefaultCellStyle.Font = FontBold
DataGridView1.Columns("DeviceID").DefaultCellStyle.Font = FontBold
DataGridView1.Columns("SerialNumber").DefaultCellStyle.Font = FontBold
GetSerialUSBDeviceID()
ColoraRigheDispariDataGridView()
End Sub
''' <summary>
''' Funzione Colora Righe
''' </summary>
''' <remarks></remarks>
Private Sub ColoraRigheDispariDataGridView()
For Each row In Me.DataGridView1.Rows.OfType(Of DataGridViewRow)()
If row.Index / 2 = Int(row.Index / 2) Then
For Each cell In row.Cells.OfType(Of DataGridViewCell)()
cell.Style.BackColor = Color.LightCyan
Next
End If
Next
End Sub
End Class
Potete scaricare il file d'esempio :
Password : "ImaginSystem"
By ImaginSystems & Queen Gin