none
Getting usb flash drive serial number in c# RRS feed

  • Question

  • hello everyone. is it possible to get usb flash drive serial key in vc#. im developing an application that will use usb flash drive to lock and unlock folders but my problem is how to get the usb flash drive serial key.  thank you. :D 
    • Edited by 7.Kenneth Wednesday, November 28, 2012 1:54 PM
    • Moved by Chester Hong Friday, November 30, 2012 8:59 AM not a WinForm development question (From:Windows Forms General)
    • Moved by Sheng Jiang 蒋晟 Sunday, December 2, 2012 4:08 PM hardware programming (From:Where is the Forum For…?)
    Wednesday, November 28, 2012 1:49 PM

Answers

All replies

  • A small App could be the answer by the name of  USBDeview.

    From CNET - "It's a small, free tool that lists all the USB devices currently attached to your PC as well as detailed information about each, including the device, manufacturer, model, serial number, VendorID and ProductID, and even the date and time the device was attached to your system."





    • Marked as answer by 7.Kenneth Thursday, November 29, 2012 1:08 AM
    • Unmarked as answer by 7.Kenneth Thursday, November 29, 2012 1:09 AM
    Wednesday, November 28, 2012 5:58 PM
  • Thank you for your answer sir but im looking for a code in vc# in getting the usb flash drive unique serial key :)
    Thursday, November 29, 2012 1:08 AM
  • Hi 7.Kenneth,

    This is Windows Forms forum about some .NET graphic libraries. I'm not sure whether there is a special forum for your question. I would like to move your thread to Where is the Forum For…? forum.

    If you can't find the forum for usb flash drive, I think you can try Visual C# General forum.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, November 30, 2012 8:58 AM
  • Search for WMI USB.

    http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives#AWalkAroundWMI might give you a few pointers.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    • Proposed as answer by Just Karl Tuesday, December 4, 2012 5:32 PM
    • Marked as answer by Doron Holan [MSFT] Saturday, December 8, 2012 4:36 PM
    Friday, November 30, 2012 2:46 PM
  • sir im new to c# may i ask if what is wmi? sir is there any code that use wndproc to get it?

    thanks sir

    Saturday, December 8, 2012 4:15 PM
  • WMI stands for Windows Management Instrumentation see http://msdn.microsoft.com/en-us/library/aa394582(v=VS.85).aspx

     

    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Saturday, December 8, 2012 4:22 PM
  • Sir what is the problem here.. this is the source code and this is the error i get 

    Error 2 The type or namespace name 'ManagementObjectSearcher' could not be found (are you missing a using directive or an assembly reference?)

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


    Sunday, December 9, 2012 1:57 AM
  • 1. Add reference "System.Management" in your project.

    2. Add "using System.Management;"  in your code file.

    Tuesday, August 27, 2013 12:03 AM