简体   繁体   English

如何根据设备ID字符串SCSI将驱动器号添加到组合框

[英]How to add a drive letter to a Combo Box based on a Device ID string of SCSI

Based on my query How to find the true CD-Rom drive letter when a virtual drive is also installed? 基于我的查询如何在安装虚拟驱动器时找到真正的CD-Rom驱动器盘符?

And the suggestion I got from here How to identify if a drive is virtual or physical 我从这里得到的建议如何识别驱动器是虚拟的还是物理的

I'd like to just fill a Combo Box of virtual drive letters when the DeviceID string contains SCSI, I've tested both my virtual drives and they do have SCSI listed against them. 当DeviceID字符串包含SCSI时,我想填充一个组合框的虚拟驱动器号,我已经测试了我的虚拟驱动器,并且它们确实列出了针对它们的SCSI。

The first 4 characters in the answer from the example in the suggested link 建议链接中示例的答案中的前4个字符

string driveLetter = "G";
        ManagementObjectSearcher diskQuery = new ManagementObjectSearcher(String.Format("SELECT * FROM Win32_CDROMDrive WHERE Drive='{0}:'", driveLetter));
        ManagementObject diskResult = diskQuery.Get().OfType<ManagementObject>().SingleOrDefault();
        string deviceID = null;
        if (diskResult != null)
            deviceID = (string)diskResult["DeviceID"];
        MessageBox.Show(deviceID);

Show SCSI, so I thought I could do something like this 显示SCSI,所以我想我可以做这样的事情

ManagementObjectSearcher diskQuery = new ManagementObjectSearcher(String.Format("select * from Win32_CDROMDrive Where DeviceID Like '%SCSI%'"));
        ManagementObject diskResult = diskQuery.Get().OfType<ManagementObject>().SingleOrDefault();
        string deviceID = null;
        if (diskResult != null)
            deviceID = (string)diskResult["DeviceID"];
        MessageBox.Show(deviceID);  

However it doesn't work, I just get an Invalid Operation Exception. 但它不起作用,我只是得到一个无效的操作例外。

What I'm trying to do is this 我想要做的就是这个

ComboBox cbVirtual = new ComboBox();
    var vdrives = DriveInfo.GetDrives();
    foreach (var drive in vdrives)
        if (drive.DriveType == DriveType.CDRom)
        {
            If the deviceID string contains SCSI
            {
                Fill the Combo box with the drive letter/s
            }
        }

Appreciate some help - cheers. 欣赏一些帮助 - 欢呼。

you need to 你需要

string driveLetter = "G";

ComboBox.ObjectCollection items = vdrives.Items;
Items.Add(driveLetter);

that's how you add objects to the Combo Box. 这是你如何添加对象到组合框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM