如何按序列号获取CD / DVD-ROM驱动器号

时间:2018-12-07 22:43:09

标签: c# drive

我正在尝试使用其提供的PTRobot API与Primera Disc Duplicator进行接口。他们的API返回有关机械手中记录器驱动器的信息,但是缺少的关键是驱动器号。

他们返回的信息是型号名称,固件和序列号。

我需要区分一个单元中的多个相同驱动器,并且序列号是唯一提供的唯一值。

我发现许多示例都可以通过使用驱动器号来获取模型或序列号,但是它们看起来都无法翻转供我使用。

3 个答案:

答案 0 :(得分:2)

您可以编写例程,通过检查每个驱动器来构建由序列号散列的驱动器字典。然后,您将获得使用PTRobot api所需的缺少信息。

编辑:

From a search for c# getting a serial number for a drive

Code from an example of how to get the hard drive serial number. UNtested as I no longer have a windows device

以下内容可以为您提供帮助:

searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
 // get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection[i];

// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
 hd.SerialNo = "None";
else
 hd.SerialNo = wmi_HD["SerialNumber"].ToString();

++i;
}

答案 1 :(得分:2)

听起来您可以获得序列号与您要搜索的序列号匹配的驱动器,然后获取其分区,对于每个分区,请从逻辑驱动器获取驱动器号。

例如:

using System.Collections.Generic
using System.Management;

public static List<string> GetDriveLettersForSerialNumber(string driveSerialNumber)
{        
    var results = new List<string>();
    if (driveSerialNumber == null) return results;

    var drive = new ManagementObjectSearcher(
        "SELECT DeviceID, SerialNumber, Partitions FROM Win32_DiskDrive").Get()
        .Cast<ManagementObject>()
        .FirstOrDefault(device =>
            device["SerialNumber"].ToString().Trim()
                .Equals(driveSerialNumber.Trim(), StringComparison.OrdinalIgnoreCase));

    if (drive == null) return results;

    var partitions = new ManagementObjectSearcher(
        $"ASSOCIATORS OF {{Win32_DiskDrive.DeviceID='{drive["DeviceID"]}'}} " +
        "WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get();

    foreach (var partition in partitions)
    {
        var logicalDrives = new ManagementObjectSearcher(
            "ASSOCIATORS OF {{Win32_DiskPartition.DeviceID=" + 
            $"'{partition["DeviceID"]}'}} " +
            "WHERE AssocClass = Win32_LogicalDiskToPartition").Get();

        foreach (var logicalDrive in logicalDrives)
        {
            var volumes = new ManagementObjectSearcher(
                "SELECT Name FROM Win32_LogicalDisk WHERE " +
                $"Name='{logicalDrive["Name"]}'").Get().Cast<ManagementObject>();

            results.AddRange(volumes.Select(v => v["Name"].ToString()));
        }
    }

    return results;
}

对于CDROM来说,似乎更容易-“ Id”和“ SerialNumber”都包含在同一个对象中:

public static string GetDriveLetterForCDROMSerialNumber(string driveSerialNumber)
{
    return new ManagementObjectSearcher(
        "SELECT Id, SerialNumber FROM Win32_CDROMDrive").Get()
        .Cast<ManagementObject>()
        .Where(drive => drive.GetPropertyValue("SerialNumber").ToString().Trim()
            .Equals(driveSerialNumber.Trim(), StringComparison.OrdinalIgnoreCase))
        .Select(drive => drive.GetPropertyValue("Id").ToString())
        .FirstOrDefault() ?? "Unknown";
}

答案 2 :(得分:0)

感谢您的建议,并指出我可以使用WMI查询。只是查找哪一个拥有我需要的信息(Win32_CDROMDrive)。这是我的工作代码:

public static string GetDriveLetter(string serialNum){
    if (serialNum != null)
    {
        var moc = new ManagementObjectSearcher("SELECT SerialNumber, Drive FROM Win32_CDROMDrive");

        foreach(var mo in moc.Get())
        {
            string driveSerial = (string)mo.GetPropertyValue("SerialNumber");
            if (driveSerial != null)
            {
                if (driveSerial.Trim().Equals(serialNum.Trim(), StringComparison.OrdinalIgnoreCase))
                {
                    return (string)mo.GetPropertyValue("Drive");
                }
            }
        }
    }
    return "Unknown";
}