C# USB Serial number, Pnp number, Description 정보 출력 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestUSB_Ver2
{
    class USBSerialNumber   //USB 정보를 가져와서 출력해주는 클래스
    {
        public void main(object Main_USB_inter)
        {
            DataGridView USB_inter = Main_USB_inter as DataGridView; // Form object 상속 받음 
            USB_inter.Rows.Clear();
            
            // USB 정보 호출 
            var usbDevices = GetUSBDevices();
            int i = 1;
            foreach(var usbDevice in usbDevices)
            {
                USB_inter.Rows.Add(i, usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
                i++;
            }
            //// 

        }
        // USB 정보 가져오는 부분 
        static List GetUSBDevices()
        {
            List devices = new List();

            ManagementObjectCollection collection;
            using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
                collection = searcher.Get();

            foreach (var device in collection)
            {
                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("DeviceID"),
                (string)device.GetPropertyValue("PNPDeviceID"),
                (string)device.GetPropertyValue("Description")
                ));
            }

            collection.Dispose();
            return devices;
        }
    }
    class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
        {
            this.DeviceID = deviceID;
            this.PnpDeviceID = pnpDeviceID;
            this.Description = description;
        }
        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
    }
    // USB 정보 가져오는 부분--끝
}


소스 : http://jmoon.co.kr/106 / jmoon1601.tistory.com/106


+ Recent posts