1

I have winform application working on USB, I distribute the application on USB for the clients, I'm checking the the USB serial number if the application moved to another USB the application shows a message to the user that he cant run this app because he is not registered, I have a method getting the USB serial number by USB letter, but the problem is windows changing the USB letter dynamically, so its hard to get the USB letter and I cannot make the letter fixed so I can't read it.

I'm looking for a way to get the serial number for the USB by the USB Name is That possible ?? and if not what is the best way to manage my problem ??

Here is the Class I use to get the USB serial number :

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;
    }

}
RabQab
  • 51
  • 4
  • 1
    Take a look at this: [Get List of connected USB Devices](https://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices). – Aleksa Ristic Sep 25 '21 at 20:05
  • 1
    [Get the serial number of USB storage devices in .Net Core 2.1](https://stackoverflow.com/a/51806262/7444103) -- Not just the *Serial Number*. Same thing with .Net Framework or other (it's not really about the .Net version). – Jimi Sep 25 '21 at 20:11
  • 1
    You can also combine that info collector with [How do I get information about recently connected USB device?](https://stackoverflow.com/a/54298316/7444103): this is used to get a notification (an event) when a USB device is inserted or removed, so you can check it on the fly (gather information right after it has been inserted, or notify the User that a required USB device has been removed). – Jimi Sep 25 '21 at 20:16
  • Thank you @AleksaRistic and Jimi I had an Idea, what If I get the app directory path and get the driver letter from it and compare the Serial number for that driver with the the serial number added to the app is that a good way to manage this ?? – RabQab Sep 25 '21 at 20:34
  • 1
    @RabQab If I am correct you want to make one application tighten to one USB drive? If that is so, lets say you hardcode given USB drive into your code. All you need to do when app starts is go through all seral codes of usb drives currently connected to machine and check if any matches hard coded serial number. If it does then let app run, otherwise shut down – Aleksa Ristic Sep 25 '21 at 20:45

0 Answers0