Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read USB Device Serial number in C#

Is there a way to read USB device serial number and data in a text file in USB using visual studio 2005?

like image 365
rahul Avatar asked Jul 24 '09 06:07

rahul


People also ask

How do I find my USB serial number?

Each USB hardware device has a unique serial number that is required during the activation process. To locate the serial number of a key, turn the key to the side opposite the colored label. You see three rows of numbers. The lowest or bottom row of numbers is the serial number.

How do I find my USB serial number Ubuntu?

You can use lsusb , but you need to add verbose flag and make sure you use sudo with it, otherwise the serial will be incorrect. Then run lsusb with device flag and grep the serial number.

How is a USB device identified?

Devices are identified by descriptors. Once the USB host has established a USB device is connected, and at what speed it should communicate, then the host will reset the USB device and attempt to read the descriptors to identify the USB device using a default address.

How do I check USB logs?

If you don't know the name of your computer, go to Settings > System > About. Your computer name is shown at the top. Click the Start button to see the USB history. You can then expand the results to see details such as the time and date it was last used.


1 Answers

Try this:

USBSerialNumber usb = new USBSerialNumber();
string serial = usb.getSerialNumberFromDriveLetter("f:\");
MessageBox.Show(serial);

Here's the internals for the USBSerialNumber class:

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace USBDriveSerialNumber {
    public class USBSerialNumber {

        string _serialNumber;
        string _driveLetter;

        public string getSerialNumberFromDriveLetter(string driveLetter) {
            this._driveLetter = driveLetter.ToUpper();

            if(!this._driveLetter.Contains(":")) {
                this._driveLetter += ":";
            }

            matchDriveLetterWithSerial();

            return this._serialNumber;
        }

        private void matchDriveLetterWithSerial() {

            string[] diskArray;
            string driveNumber;
            string driveLetter;

            ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
            foreach (ManagementObject dm in searcher1.Get()) {
                diskArray = null;
                driveLetter = getValueInQuotes(dm["Dependent"].ToString());
                diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(',');
                driveNumber = diskArray[0].Remove(0, 6).Trim();
                if(driveLetter==this._driveLetter){
                    /* This is where we get the drive serial */
                    ManagementObjectSearcher disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
                    foreach (ManagementObject disk in disks.Get()) {

                        if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB") {
                            this._serialNumber = parseSerialFromDeviceID(disk["PNPDeviceID"].ToString());
                        }
                    }
                }
            }
        }

        private string parseSerialFromDeviceID(string deviceId) {
            string[] splitDeviceId = deviceId.Split('\\');
            string[] serialArray;
            string serial;
            int arrayLen = splitDeviceId.Length-1;

                serialArray = splitDeviceId[arrayLen].Split('&');
                serial = serialArray[0];

            return serial;
        }

        private string getValueInQuotes(string inValue) {
            string parsedValue = "";

            int posFoundStart = 0;
            int posFoundEnd = 0;

            posFoundStart = inValue.IndexOf("\"");
            posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1);

            parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);

            return parsedValue;
        }

    }
}

Source: http://www.cfdan.com/posts/Retrieving_Non-Volatile_USB_Serial_Number_Using_C_Sharp.cfm

like image 163
The Matt Avatar answered Sep 27 '22 21:09

The Matt