簡體   English   中英

如何在不接收空字符串的情況下獲取主板 ID?

[英]How to get motherboard ID without receiving empty strings?

我嘗試了很多東西:

//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;
}

這在我的 PC 上都給出了空字符串,但在筆記本電腦上給出了正確的 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# 的工作解決方案

有些主板根本沒有ID。 它設置為空字符串。 因此,如果有人需要將主板獨特的東西用於許可目的,他們應該收到主板 UUID。

就個人而言,我建議使用這個特定的開源硬件監視器庫(您將需要源代碼)。 您可以將其用於硬件識別。 打開硬件監視器

還有一個名為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();

願原力與你同在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM