can not get hardware information in .net core desktop application

i want to get cpu and motherboard ( and some time hardware) id so i use below code :

public class SystemKey : ISystemKey
{
    public string GetDeviceId(int Length)
    {
        var uniqueKey = GetDeviceId();
        if (uniqueKey.Length >= Length)
            return uniqueKey.Substring(0, Length);

        var key = new StringBuilder(uniqueKey);
        for (int counter = uniqueKey.Length; counter <= Length; counter++)
        {
            var i = Length - uniqueKey.Length;
            key.Append(uniqueKey[i]);
        }
        return key.ToString();
    }

    public string GetDeviceId()
    {
        string uniqueKey; 
        try
        {
            uniqueKey = GetCpuSerialNumbewr() + GetMotherBoardID();
        }
        catch
        {
            ISystemKey alternativeSystemKey = new AlternativeSystemKey();
            uniqueKey = alternativeSystemKey.GetDeviceId(); 
        }
        if(string.IsNullOrEmpty(uniqueKey))
        {
            throw new Exception("can not get device id for current system"); 
        }
        return uniqueKey;
    }

    private string GetCpuSerialNumbewr()
    {
        List<string> results = new List<string>();
        string query = "Select * FROM Win32_Processor";
        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(query);
        foreach (ManagementObject info in searcher.Get())
        {
            results.Add(
                info.GetPropertyValue("ProcessorId").ToString());
        }

        return results.FirstOrDefault();

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

and alternative system key (in above code invoked) :

public class AlternativeSystemKey : ISystemKey
{
    private static string identifier(string wmiClass, string wmiProperty)
    {
        string result = "";
        System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
        System.Management.ManagementObjectCollection moc = mc.GetInstances();
        foreach (System.Management.ManagementObject mo in moc)
        {
            //Only get the first one
            if (result == "")
            {
                try
                {
                    result = mo[wmiProperty].ToString();
                    break;
                }
                catch
                {
                }
            }
        }
        return result;
    }
    private static string cpuId()
    {
        //Uses first CPU identifier available in order of preference
        //Don't get all identifiers, as it is very time consuming
        string retVal = identifier("Win32_Processor", "UniqueId");
        if (retVal == "") //If no UniqueID, use ProcessorID
        {
            retVal = identifier("Win32_Processor", "ProcessorId");
            if (retVal == "") //If no ProcessorId, use Name
            {
                retVal = identifier("Win32_Processor", "Name");
                if (retVal == "") //If no Name, use Manufacturer
                {
                    retVal = identifier("Win32_Processor", "Manufacturer");
                }
                //Add clock speed for extra security
                retVal += identifier("Win32_Processor", "MaxClockSpeed");
            }
        }
        return retVal;
    }
    //BIOS Identifier
    private static string biosId()
    {
        return identifier("Win32_BIOS", "Manufacturer")
        + identifier("Win32_BIOS", "SMBIOSBIOSVersion")
        + identifier("Win32_BIOS", "IdentificationCode")
        + identifier("Win32_BIOS", "SerialNumber")
        + identifier("Win32_BIOS", "ReleaseDate")
        + identifier("Win32_BIOS", "Version");
    }
    //Main physical hard drive ID
    private static string diskId()
    {
        return identifier("Win32_DiskDrive", "Model")
        + identifier("Win32_DiskDrive", "Manufacturer")
        + identifier("Win32_DiskDrive", "Signature")
        + identifier("Win32_DiskDrive", "TotalHeads");
    }
    //Motherboard ID
    private static string baseId()
    {
        return identifier("Win32_BaseBoard", "Model")
        + identifier("Win32_BaseBoard", "Manufacturer")
        + identifier("Win32_BaseBoard", "Name")
        + identifier("Win32_BaseBoard", "SerialNumber");
    }
    //Primary video controller ID
    private static string videoId()
    {
        return identifier("Win32_VideoController", "DriverVersion")
        + identifier("Win32_VideoController", "Name");
    }

    public string GetDeviceId(int length)
    {
        throw new NotImplementedException();
    }

    public string GetDeviceId()
    {
        return baseId() + cpuId() + biosId();
    }
}

so in this two class we have some method , but when we use them in some systems is not work(exception) and the error say : "microsoft.net\framework\v4.030319\wminet_utils does not have all required functions. please update the .net framework" but the question is that : "why this exception accur ? because application written with .net core and published as self contained , it does not means application should work without any dependency ? summary question :

  1. is there any compatible .net core code for getting hardware information ? 2)why when application published as self contained it say's : "i need more dependency to install" ? 3)best way for resolve above problem (srry for bad english)


Read more here: https://stackoverflow.com/questions/68458928/can-not-get-hardware-information-in-net-core-desktop-application

Content Attribution

This content was originally published by sajad at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: