C# でハードウェア ID を高速に取得するにはどうすればよいですか?
-
22-09-2019 - |
質問
プログラムでライセンスをハードウェア ID に結び付ける必要があります。WMIを使ってみましたが、やはり遅いです。
たとえば、CPU、HDD、マザーボードの情報が必要です。
解決
詳細については、を参照してください。 このリンク
次のコードにより CPU ID が得られます。
名前空間が必要です System.Management
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;
}
ハードディスク ID とマザーボード ID の詳細については、を参照してください。 このリンク
この手順を高速化するには、次のコマンドを使用しないようにしてください。 SELECT *
, ただし、本当に必要なものだけを選択してください。使用 SELECT *
開発中にのみ、何を使用する必要があるかを調べようとする場合にのみ使用してください。クエリには時間がかかるためです。 多くの 完了までにさらに時間がかかります。
他のヒント
私はこのことか別の解決策です。ばんに興味のある私と共に、このクラス:
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
}
}
んですが、受信用のためのこなかったです。 こちらの うまくいったよりも早く見ました。のグラフィックカード、mac、ドidのんのユニークなIDの約2-3秒です。以上含むにはログインしてくださ約4~5秒です。
注意: 追加参照 System.Management
.
は、以下のアプローチが触発された関連(より一般的な)質問への答えは、このでます。
のアプローチは、レジストリキーのMachineGuid
でHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography
値を読むことです。この値は、OSのインストール時に生成されます。
このアプローチを使用してマシンごとのハードウェアIDの一意の周りのいくつかの方法があります。一つの方法は、レジストリ値を編集しているが、これはその後、ユーザーのマシン上の合併症を引き起こします。もう一つの方法は、MachineGuid
値をコピーするドライブイメージのクローンを作成することです。
しかし、アプローチがハックプルーフではありません、これは確かに、通常のユーザーのために良い十分でしょう。プラス側では、このアプローチは、迅速な性能面および実装するために簡単です。
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();
}
}
}
ここには、DLLがあることを示している。
*ハードドライブのID(固有のハードウェアシリアル番号は、ドライブのIDE電子チップに書かれた)
*パーティションID(ボリュームシリアル番号)
* CPUのID(一意のハードウェアID)
* CPUベンダー
* CPU走行速度
* CPU理論スピード
*メモリロード(割合(%)で使用される合計メモリー)
*合計物理(バイト単位の合計物理メモリ)
* AVAIL物理(物理メモリがバイト単位で左)
*合計ページファイル(バイト単位での総ページファイル)
*利用可能なページファイル(バイト単位で左ページファイル)
*合計仮想(バイト単位の合計仮想メモリ)
*利用可能な仮想(バイト単位で左仮想メモリ)
*ビオス固有の識別numberBiosDate
*ビオス固有の識別numberBiosVersion
*ビオス固有の識別numberBiosProductID
*ビオス固有の識別numberBiosVideo
(テキストは、元のウェブサイトから取得)
これは、C#で動作します。