Question

J'ai besoin dans mon programme pour attacher une licence à un ID de matériel. J'ai essayé d'utiliser WMI, mais il reste lent.

J'ai besoin, par exemple, CPU, disque dur, et les informations carte mère.

Était-ce utile?

La solution

Pour plus de détails, voir href="http://jai-on-asp.blogspot.com/2010/03/finding-hardware-id-of-computer.html" ce lien

Le code suivant vous donnera CPU ID:

namespace System.Management requis

var mbs = new ManagementObjectSearcher("Select ProcessorId From Win32_processor");
ManagementObjectCollection mbsList = mbs.Get();
string id = "";
foreach (ManagementObject mo in mbsList)
{
    id = mo["ProcessorId"].ToString();
    break;
}

Pour disque dur ID et les détails de la carte mère id font référence ce lien-

Pour accélérer cette procédure, assurez-vous de ne pas utiliser SELECT *, mais sélectionnez uniquement ce que vous avez vraiment besoin. Utilisez SELECT * seulement au cours du développement lorsque vous essayez de savoir ce que vous devez utiliser, parce que la requête prendra beaucoup plus longtemps.

Autres conseils

Je suis arrivé ici à la recherche de la même chose et j'ai trouvé une autre solution. Si vous les gars sont intéressés je partage cette classe:

using System;
using System.Management;
using System.Security.Cryptography;
using System.Security;
using System.Collections;
using System.Text;
namespace Security
{
    /// <summary>
    /// Generates a 16 byte Unique Identification code of a computer
    /// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9
    /// </summary>
    public class FingerPrint  
    {
        private static string fingerPrint = string.Empty;
        public static string Value()
        {
            if (string.IsNullOrEmpty(fingerPrint))
            {
                fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " + 
            biosId() + "\nBASE >> " + baseId() +
                            //"\nDISK >> "+ diskId() + "\nVIDEO >> " + 
            videoId() +"\nMAC >> "+ macId()
                                     );
            }
            return fingerPrint;
        }
        private static string GetHash(string s)
        {
            MD5 sec = new MD5CryptoServiceProvider();
            ASCIIEncoding enc = new ASCIIEncoding();
            byte[] bt = enc.GetBytes(s);
            return GetHexString(sec.ComputeHash(bt));
        }
        private static string GetHexString(byte[] bt)
        {
            string s = string.Empty;
            for (int i = 0; i < bt.Length; i++)
            {
                byte b = bt[i];
                int n, n1, n2;
                n = (int)b;
                n1 = n & 15;
                n2 = (n >> 4) & 15;
                if (n2 > 9)
                    s += ((char)(n2 - 10 + (int)'A')).ToString();
                else
                    s += n2.ToString();
                if (n1 > 9)
                    s += ((char)(n1 - 10 + (int)'A')).ToString();
                else
                    s += n1.ToString();
                if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
            }
            return s;
        }
        #region Original Device ID Getting Code
        //Return a hardware identifier
        private static string identifier
        (string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            System.Management.ManagementClass mc = 
        new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                if (mo[wmiMustBeTrue].ToString() == "True")
                {
                    //Only get the first one
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return result;
        }
        //Return a hardware identifier
        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");
        }
        //First enabled network card ID
        private static string macId()
        {
            return identifier("Win32_NetworkAdapterConfiguration", 
                "MACAddress", "IPEnabled");
        }
        #endregion
    }
}

Je ne prendrai aucun crédit pour cela parce que je l'ai trouvé ici Il a travaillé plus vite que prévu pour moi. Sans la carte graphique, Mac et conduire id je suis l'ID unique dans environ 2-3 secondes. Avec ceux inclus ci-dessus je l'ai dans environ 4-5 secondes.

Remarque:. Ajouter référence à System.Management

L'approche suivante a été inspirée par cette réponse à une question connexe (plus générale).

L'approche est de lire la valeur MachineGuid dans le registre HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography clé. Cette valeur est générée lors de l'installation OS.

Il y a quelques façons de contourner le caractère unique du matériel ID par machine en utilisant cette approche. Une méthode est d'éditer la valeur de Registre, mais cela entraînerait des complications sur la machine de l'utilisateur après. Une autre méthode consiste à cloner une image disque qui copie la valeur MachineGuid.

Cependant, aucune approche hack preuve et ce sera certainement assez bon pour les utilisateurs normaux. Du côté positif, cette approche est la performance sage rapide et simple à mettre en œuvre.

public string GetMachineGuid()
{
   string location = @"SOFTWARE\Microsoft\Cryptography";
   string name = "MachineGuid";

   using (RegistryKey localMachineX64View = 
       RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
   {
       using (RegistryKey rk = localMachineX64View.OpenSubKey(location))
       {
           if (rk == null)
               throw new KeyNotFoundException(
                   string.Format("Key Not Found: {0}", location));

           object machineGuid = rk.GetValue(name);
           if (machineGuid == null)
               throw new IndexOutOfRangeException(
                   string.Format("Index Not Found: {0}", name));

           return machineGuid.ToString();
       }
   }
}

est une DLL qui montre:
* ID du disque dur (numéro de série matériel unique écrit en puce électronique IDE de lecteur)
* ID de partition (volume numéro de série)
* CPU ID (ID matériel unique)
* CPU fournisseur
* CPU vitesse de fonctionnement
* Vitesse CPU
théorétique * La charge de la mémoire (mémoire totale utilisée en pourcentage (%))
* Le total physique (Total mémoire physique en octets)
* Physique Avail (mémoire physique gauche en octets)
* Le total PageFile (fichier total de pages en octets)
* PageFile (fichier Page de gauche en octets) Disponible
* Le total virtuel (mémoire virtuelle totale en octets)
* Virtual (mémoire virtuelle gauche en octets) Disponible
* Bios identification unique numberBiosDate
* Bios identification unique numberBiosVersion
* Bios identification unique numberBiosProductID
* Bios identification unique numberBiosVideo

(texte saisi à partir du site Web d'origine)
Il fonctionne avec C #.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top