0

コンピューターに接続されている特定の USB デバイス (1 つ以上) を検索し、マウントされたドライブへの関連パスを取得しようとしています。理想的には、USB デバイスの VID/PID を見つけることですが、その方法はまだわかりません。以下は機能しますが、単一のクエリでデータを取得する方法が必要です。

ここで行っているのは、モデルが一致するHS SD Card Bridge USB Device物理ドライブを探して、関連付けられている物理ドライブ番号を見つけ、それを使用してマウントされたパーティションを見つけることです..

        foreach (ManagementObject disk in disks.Get()) {
            //look for drives that match our string
            Match m = Regex.Match(disk["model"].ToString(), "HS SD Card Bridge USB Device");
            if (m.Success) {
                m = Regex.Match(disk["DeviceID"].ToString(), @"PHYSICALDRIVE(\d+)");
                if (m.Success) {
                    int driveNumber = Int32.Parse(m.Groups[1].ToString());
                    ManagementObjectSearcher mapping = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
                    foreach (ManagementObject map in mapping.Get()) {
                        m = Regex.Match(map["Antecedent"].ToString(), @"Disk #" + driveNumber + ",");
                        if (m.Success) {
                            string drive = map["Dependent"].ToString();
                            m = Regex.Match(drive, @"([A-Z]):");
                            if (m.Success) {
                                drive = m.Groups[1].ToString(); //< -- **FOUND**
                            }
                        }

                    }
                    //USBDevice dev = new USBDevice("", "");
                    //  list.Items.Add();
                    Console.WriteLine("");
                }
            }
}

VID/PID からこれを行う方法と、1 つのクエリだけで済むように検索クエリを作成する方法はありますか?

4

1 に答える 1

0

これは私が以前に使用したものです。これでは答えになりません。しかし、あなたを助けます。

public int GetAvailableDisks()
        {
            int  deviceFound = 0;
            try
            {
                // browse all USB WMI physical disks
                foreach (ManagementObject drive in
                    new ManagementObjectSearcher(
                        "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get())
                {
                    ManagementObject partition = new ManagementObjectSearcher(String.Format(
                        "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition",
                        drive["DeviceID"])).First();
                    if (partition == null) continue;
                    // associate partitions with logical disks (drive letter volumes)
                    ManagementObject logical = new ManagementObjectSearcher(String.Format(
                        "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition",
                        partition["DeviceID"])).First();
                    if (logical != null)
                    {
                        // finally find the logical disk entry to determine the volume name - Not necesssary 
                        //ManagementObject volume = new ManagementObjectSearcher(String.Format(
                        //    "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'",
                        //    logical["Name"])).First();

                        string temp = logical["Name"].ToString() + "\\";
                        // +" " + volume["VolumeName"].ToString(); Future purpose if Device Name required

                        deviceFound++;
                        if (deviceFound > 1)
                        {
                            MessageBox.Show(@"Multiple Removeable media found. Please remove the another device");
                            deviceFound--;

                        }
                        else
                        {
                            driveName = temp;
                        }

                    }
                }
            }
            catch (Exception diskEnumerateException)
            {

            }
            return deviceFound;
        }
于 2013-08-22T06:25:53.707 に答える