如何获取主板ID而不接收空字符串?

时间:2013-07-29 14:38:59

标签: c# c++ motherboard

我尝试了很多东西:

//public static string GetMotherBoardID()
//{
//    string mbInfo = String.Empty;

//    //Get motherboard's serial number 
//    ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
//    foreach (ManagementObject mo in mbs.Get())
//        mbInfo += mo["SerialNumber"].ToString();

//    return mbInfo;
//}

//public static string GetMotherBoardID()
//{
//    string mbInfo = String.Empty;
//    ManagementScope scope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
//    scope.Connect();
//    ManagementObject wmiClass = new ManagementObject(scope, new ManagementPath("Win32_BaseBoard.Tag=\"Base Board\""), new ObjectGetOptions());

//    foreach (PropertyData propData in wmiClass.Properties)
//    {
//        if (propData.Name == "SerialNumber")
//            mbInfo = String.Format("{0,-25}{1}", propData.Name, Convert.ToString(propData.Value));
//    }

//    return mbInfo;
//}

public static string GetMotherBoardID()
{
    string mbInfo = String.Empty;
    ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
    ManagementObjectCollection moc = mbs.Get();
    ManagementObjectCollection.ManagementObjectEnumerator itr = moc.GetEnumerator();

    itr.MoveNext();
    mbInfo = itr.Current.Properties["SerialNumber"].Value.ToString();

    var enumerator = itr.Current.Properties.GetEnumerator();

    if (string.IsNullOrEmpty(mbInfo))
        mbInfo = "0";

    return mbInfo;
}

这一切都在我的电脑上显示空字符串,但在笔记本电脑上显示正确的ID。 其他一些人也在两台PC上报告是空主板ID。

结果:

public static string GetMotherBoardID()
{
    string mbInfo = String.Empty;
    ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
    ManagementObjectCollection moc = mbs.Get();
    ManagementObjectCollection.ManagementObjectEnumerator itr = moc.GetEnumerator();

    itr.MoveNext();
    mbInfo = itr.Current.Properties["SerialNumber"].Value.ToString();

    var enumerator = itr.Current.Properties.GetEnumerator();

    string properties = "";

    while (enumerator.MoveNext())
    {
        properties += "[" + enumerator.Current.Name + "][" + (enumerator.Current.Value != null ? enumerator.Current.Value.ToString() : "NULL") + "]\n";
    }

    if (string.IsNullOrEmpty(mbInfo))
        mbInfo = "0";

    return mbInfo;
}
[Caption][Основная плата]
[ConfigOptions][NULL]
[CreationClassName][Win32_BaseBoard]
[Depth][NULL]
[Description][Основная плата]
[Height][NULL]
[HostingBoard][True]
[HotSwappable][False]
[InstallDate][NULL]
[Manufacturer][Gigabyte Technology Co., Ltd.]
[Model][NULL]
[Name][Основная плата]
[OtherIdentifyingInfo][NULL]
[PartNumber][NULL]
[PoweredOn][True]
[Product][H55M-S2H]
[Removable][False]
[Replaceable][True]
[RequirementsDescription][NULL]
[RequiresDaughterBoard][False]
[SerialNumber][ ]
[SKU][NULL]
[SlotLayout][NULL]
[SpecialRequirements][NULL]
[Status][OK]
[Tag][Base Board]
[Version][x.x]
[Weight][NULL]
[Width][NULL]

也许c#不适合检索这些东西? 我希望C / C ++上的解决方案或C#上的工作解决方案

3 个答案:

答案 0 :(得分:1)

就个人而言,我建议使用这个特定的开源硬件监控库(您需要源代码)。您可以将其用于硬件识别。 Open Hardware Monitor

答案 1 :(得分:1)

有些主板根本没有ID。它设置为空字符串。 因此,如果有人需要使用主板独特的东西用于许可目的,他们应该收到主板UUID。

答案 2 :(得分:0)

还有一个名为 DeviceID 的 NuGet 包。但是,您需要在包中包含他们的 DLL,但这是一个非常快速、简单的解决方案。

这是一个用法示例:

 /* Depends on https://www.nuget.org/packages/DeviceId/ Install-Package DeviceId - Version 5.2.0*/
/* Using AddMacAddress(true, true) to exclude both virtual and wireless network adapters. */ 

readonly string MachineSupportID = new DeviceIdBuilder()
    .AddMacAddress(true, true)
    .AddMotherboardSerialNumber()
    .AddProcessorId()
    .AddSystemDriveSerialNumber()
    .ToString();

愿原力与你同在。