从注册表获取USB驱动器号 [英] Get USB drive letter from registry

查看:175
本文介绍了从注册表获取USB驱动器号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在这里得到PID和VID为在USB驱动器插入:

I'm looking here to get the PID and VID for a plugged in USB drive:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR\枚举

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR\Enum

问题是当插入多个驱动器时,我需要知道每个案例的驱动程序字母是什么,因为我在特定的USB驱动器上查找加密的文件使用PID和VID。

Problem is that when there is more than one drive plugged in I need to know what the driver letter is for each case because I look for a file on a specific USB drive that is encrypted with the PID and VID.

例如,在下图中,0和1的驱动程序字母是什么(VID_0930和VID_0951)

For example, in the picture below, what is the driver letter for 0 and 1 (VID_0930 and VID_0951)

我想遍历所有这些插入的驱动器并检查驱动器上是否存在该文件,如果是,请获取该驱动器的PID和VID。

I want to loop through all these plugged in drives and check if the file exists on the drive, if so, get the PID and VID of that drive.

            string zeros ="" ;;

            drives = DriveInfo.GetDrives();

            for(int i = 0; i< drives.Length; i ++)

            {

                driveInfo = drives [i];&
                //MessageBox.Show(driveInfo.ToString());

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; //&安培;&安培; driveInfo.DriveType == DriveType.Removable

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //&安培;&安培; driveInfo.DriveType = DriveType.Unknown

&NBSP;!&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //如果(driveInfo.IsReady)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; // {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //&NBSP; MessageBox.Show(driveInfo.ToString());

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; DL = driveInfo.ToString();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; // MessageBox.Show(DL + QUOT; TechKey.txt");

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;如果(File.Exists(DL + QUOT; TechKey.txt"))

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; techkey = DL + QUOT; TechKey.txt英寸;

            string zeros = "";
            drives = DriveInfo.GetDrives();
            for (int i = 0; i < drives.Length; i++)
            {
                driveInfo = drives[i];
                //MessageBox.Show(driveInfo.ToString());
                //&& driveInfo.DriveType == DriveType.Removable
                //&& driveInfo.DriveType != DriveType.Unknown
                //if (driveInfo.IsReady)
               // {
                    //  MessageBox.Show(driveInfo.ToString());
                    dl = driveInfo.ToString();
               // MessageBox.Show(dl + "TechKey.txt");
                    if (File.Exists(dl + "TechKey.txt"))
                    {
                        techkey = dl + "TechKey.txt";

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //这里我需要为如果多于一个驱动器已插入编码

                        // here I need to code for if more than one drive is plugged in.

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; RegValue = Registry.GetValue(@" HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR\Enum"," 0" ;,
0)的ToString();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;串[] RV = RegValue.Split(QUOT; \\" .ToCharArray());

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; // RV [1]持有我们的VID和放大器; PID控制,让我们去得到它

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //让我们从&中拆分Vid和Pid的字符串其间

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;串[] RV2 = RV [1] .Split(QUOT;&安培;" .ToCharArray());

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //现在Vid和Pid单独在一个数组中。让我们得到他们的人数

&NBSP;!&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; //首先VID

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;匹配VID = Regex.Match(RV2 [0],@"(小于?= VID_)\w *");

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //现在的Pid

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;匹配Pid = Regex.Match(rv2 [1],@"(?< = PID_)\ w *");

       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //&NBSP;存储的序列号唯一(不包括VID和PID)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;串RealSerial = RV [2];



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //指定原始串行

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; OriginalSerial = RealSerial;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; //修剪串口上的左字符> 16

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;如果(RealSerial.Length == 16)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; TechEnd = RealSerial;



                &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;如果(RealSerial.Length> 16)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; TechEnd官方= RealSerial.Substring(RealSerial.Length - 16);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //mystring.Substring(mystring.Length - 4);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }

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

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; TechEnd官方=零+ RealSerial;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //mystring.Substring(mystring.Length - 4);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }




               ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //MessageBox.Show(RealSerial + >> 1:" + RealSerial.Length);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //MessageBox.Show("Tech End" + TechEnd);



           ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; CompleteSerial = Vid.ToString()+ Pid.ToString()+ TechEnd官方;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //MessageBox.Show(CompleteSerial.ToString()+"是你内部密钥的完整序列号);)


$
<登记/>


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; }

                        RegValue = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR\Enum", "0", 0).ToString();
                        string[] rv = RegValue.Split("\\".ToCharArray());
                        //rv[1] holds our Vid&Pid, lets go get it.
                        //Lets split the string of Vid and Pid from & inbetween
                        string[] rv2 = rv[1].Split("&".ToCharArray());
                        //Now Vid and Pid are in an array alone. Lets get their Numbers!
                        //First Vid
                        Match Vid = Regex.Match(rv2[0], @"(?<=VID_)\w*");
                        //Now Pid
                        Match Pid = Regex.Match(rv2[1], @"(?<=PID_)\w*");
                        //  Store the serial number only (excluding VID and PID)
                        string RealSerial = rv[2];

                        //Assign Original Serial
                        OriginalSerial = RealSerial;
                        //trim left characters off the serial > 16
                        if (RealSerial.Length == 16)
                        {
                            TechEnd = RealSerial;

                        }

                        if (RealSerial.Length > 16)
                        {
                            TechEnd = RealSerial.Substring(RealSerial.Length - 16);
                            //mystring.Substring(mystring.Length - 4);
                        }
                        if (RealSerial.Length < 16)
                        {
                            for (i = 0; i < (16-RealSerial.Length);i++)
                                {
                                    zeros = zeros + "0";
                                }
                            TechEnd = zeros + RealSerial;
                            //mystring.Substring(mystring.Length - 4);
                        }


                    //MessageBox.Show(RealSerial + "L: " + RealSerial.Length);
                    
                    //MessageBox.Show("Tech End" + TechEnd);

                        CompleteSerial = Vid.ToString() + Pid.ToString() + TechEnd;
                        //MessageBox.Show(CompleteSerial.ToString() + "is your complete serial for the key inside");




                    }

推荐答案

您应该查看"HKEY_LOCAL_MACHINE \SYSTEM \MountDevices""使用以"\DosDevices \ *"开头的键,其中"*"表示"*"。是驱动器号。

You should be looking at "HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices" with keys beginning with "\DosDevices\*", where the "*" is drive letter.

虽然类型是REG_BINARY,所以你可能认为它是你无法理解的东西,如果你双击它就会发现它是

UTF-16
字节序列,如"HKEY_LOCAL_MACHINE \SYSTEM\CurrentControlSet \services\volsnap\ Enum"中所示减去"STORAGE\Volume \"字首。您可以使用

UnicodeEncoding.GetString()
(如果要在以下步骤中使用.StartWith(),请注意使用.TrimStart()删除BOM。

Although the type is REG_BINARY so you might think it's something you can't understand, if you double click on it you'll find it's the UTF-16 byte sequence as in "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\volsnap\Enum" minus the "STORAGE\Volume\" prefix. You can convert the value with UnicodeEncoding.GetString() (Note the usage of .TrimStart() to remove BOM if you want to use .StartWith() in the following step).

查找包含"USBSTOR"的字符串。在此树中,然后反转该过程以获取所需的驱动器号。

Look for string containing "USBSTOR" in this tree, then reverse the process to get the drive letter you need.


这篇关于从注册表获取USB驱动器号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆