How to fast get Hardware-ID in C#?
For more details refer to this link
The following code will give you CPU ID:
namespace required 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;
}
For Hard disk ID and motherboard id details refer this-link
To speed up this procedure, make sure you don't use SELECT *
, but only select what you really need. Use SELECT *
only during development when you try to find out what you need to use, because then the query will take much longer to complete.
Create a robust hardware serial / hardware ID in C#
Your approach is generally correct. Collect different identifiers, store them securely and issue the license. Whenever only a few of the identifiers have changed, you update your storage to the new values, but still run the application.
As of the time to collect HW info, you may start loading the application in "locked mode". Once HW identifiers become available, you check the license and asynchronously unlock the application. This way you are not so limited by the communication delay - I used this approach with license validation against a remote server.
UnityEngine .NET 3.5 Get HardwareID without System.Management
Use SystemInfo.deviceUniqueIdentifier
it is built in to unity. One thing to check however is it can return the value of SystemInfo.unsupportedIdentifier
if it is running on a platform that does not provide a way to get a unique id.
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start () {
if (SystemInfo.unsupportedIdentifier != SystemInfo.deviceUniqueIdentifier)
{
// use SystemInfo.deviceUniqueIdentifier
}
}
}
Be sure you are running Unity 4.6.2 or newer as there was a bug for it on linux not returning consistent values but it was fixed in 4.6.2.
How do I generate a unique number for every computer?
You realistically have MotherboardID, CPUID, Disk Serial and MAC address, from experience none of them are 100%.
Our stats show
- Disk serial Is missing 0.1 %
- MAC Is missing 1.3 %
- Motherboard ID Is missing 30 %
- CPUID Is missing 99 %
0.04% of machines tested they yielded no information, we couldn't even read the computer name. It maybe that these were some kind over virtual PC, HyperV or VMWare instance?
Disk serial is the most reliable, but easy to change, mac can be changed and depending on the filtering applied can change if device drivers are added (hyperv, wireshark etc).
Motherboard and CPUID sometimes return values that are placeholders "NONE" etc.
You should also note that these functions can be very slow to call (they may take a few seconds even on a fast PC), so it may be worth kicking them off on a background thread as early as possible, you ideally don't want to be blocking on them.
Motherboard ID
private static void FetchMotherboardIdInternal()
{
try
{
ManagementScope scope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
scope.Connect();
using (ManagementObject wmiClass = new ManagementObject(scope, new ManagementPath("Win32_BaseBoard.Tag=\"Base Board\""), new ObjectGetOptions()))
{
object motherboardIDObj = wmiClass["SerialNumber"];
if (motherboardIDObj != null)
{
string motherboardID = motherboardIDObj.ToString().Trim();
Trace.WriteLine("MotherboardID = " + motherboardID);
if (IsValidMotherBoardID(motherboardID))
{
_motherboardID = motherboardID;
}
}
}
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception ex)
{
Trace.TraceWarning("Failed to read MotherbaordID\r\n" + ex.Message);
}
}
public static bool IsValidMotherBoardID(string value)
{
if (value == null)
return false;
string motherboardID = value.Trim();
return !( motherboardID.Replace(".", "").Replace(" ", "").Replace("\t", "").Trim().Length < 5 ||
motherboardID.ToUpper().Contains("BASE") ||
motherboardID.Contains("2345") ||
motherboardID.ToUpper().StartsWith("TO BE") ||
motherboardID.ToUpper().StartsWith("NONE") ||
motherboardID.ToUpper().StartsWith("N/A") ||
motherboardID.ToUpper().Contains("SERIAL") ||
motherboardID.ToUpper().Contains("OEM") ||
motherboardID.ToUpper().Contains("AAAAA") ||
motherboardID.ToUpper().Contains("ABCDE") ||
motherboardID.ToUpper().Contains("XXXXX") ||
motherboardID.ToUpper().Contains("NOT") ||
motherboardID.ToUpper().StartsWith("00000")
);
}
CPU ID
private static void FetchCpuIdInternal()
{
try
{
using (ManagementClass mc = new ManagementClass("Win32_Processor"))
{
using (ManagementObjectCollection moc = mc.GetInstances())
{
foreach (ManagementObject mo in moc)
{
if (mo.Properties["UniqueId"] != null && mo.Properties["UniqueId"].Value != null)
{
// only return cpuInfo from first CPU
Trace.WriteLine("CPU ID = " + mo.Properties["UniqueId"].Value.ToString());
_cpuID = mo.Properties["UniqueId"].Value.ToString();
}
mo.Dispose();
}
}
}
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception ex)
{
Trace.TraceWarning("Failed to read CPUID\r\n" + ex.Message);
}
}
MAC Adress of first card
private static void FecthMACAddressInternal()
{
try
{
using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
using (ManagementObjectCollection moc = mc.GetInstances())
{
if (moc != null)
{
foreach (ManagementObject mo in moc)
{
Trace.WriteLine(mo["Index"] + " Mac " + mo["Caption"] + " : " + mo["MacAddress"] + " Enabled " + (bool)mo["IPEnabled"]);
if (string.IsNullOrEmpty(_macAdderss)) // only return MAC Address from first card
{
if ( mo["MacAddress"] != null && mo["IPEnabled"] != null && (bool)mo["IPEnabled"] == true)
{
_macAdderss = mo["MacAddress"].ToString();
}
}
mo.Dispose();
}
}
}
}
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception ex)
{
Trace.TraceWarning("Failed to read DiskID\r\n" + ex.Message);
}
if (_macAdderss != null)
_macAdderss = _macAdderss.Replace(":", "");
}
Drive Serial Number
/// <summary>
/// return Volume Serial Number from hard drive
/// </summary>
/// <param name="strDriveLetter">[optional] Drive letter</param>
/// <returns>[string] VolumeSerialNumber</returns>
public static string GetVolumeSerial(char driveLetter)
{
try
{
using (ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + driveLetter + ":\""))
{
if (disk == null)
return null;
disk.Get();
object diskObj = disk["VolumeSerialNumber"];
if (diskObj != null)
return diskObj.ToString();
}
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception ex)
{
Trace.TraceWarning("Failed to read DiskID\r\n" + ex.Message);
}
try
{
uint serialNum, serialNumLength, flags;
StringBuilder volumename = new StringBuilder(256);
StringBuilder fstype = new StringBuilder(256);
bool ok = GetVolumeInformation(driveLetter.ToString() + ":\\", volumename, (uint)volumename.Capacity - 1, out serialNum, out serialNumLength, out flags, fstype, (uint)fstype.Capacity - 1);
if (ok)
{
return string.Format("{0:X4}{1:X4}", serialNum >> 16, serialNum & 0xFFFF);
}
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception ex2)
{
Trace.TraceWarning("Failed to read DiskID\r\n" + ex2.Message);
}
return null;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern bool GetVolumeInformation(string Volume, StringBuilder VolumeName, uint VolumeNameSize, out uint SerialNumber, out uint SerialNumberLength, out uint flags, StringBuilder fs, uint fs_size);
I am trying to get an unique CPU ID
I came to the conclusion that this is just not a good method to get unique ID's.
So instead of this method, I wrote a much larger piece of code in which I requested much more hardware identifiers of the system to build an unique identifier.
Its way better as this method would even produce different numbers if I would run it on cloned hardware, or if hardware gets changed. There is a lot of WMI info around and I think that's the place where people should look for creating something unique and not use ProcessorID.
get hardware id using wpf
WTF?
WPF should be able to use WMI.
Did you try manually adding a reference to System.Management (WMI)?
can not get hardware information in .net core desktop application
we did not find any solution but how we resolved it ?
actulay when we creating a .net framework app and try to get hardware information there is no problem (the code does not work in .net core(system.managment version4/5/6) so we created an .net framework console application just for getting hardware information and invoke that application in new proccess and reading .net framework console app new line result , the code example :
protected string GetDeviceId(int length)
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "WpfApp.IdGenerator.exe",
Arguments = $"{length}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
return proc.StandardOutput.ReadLine();
}
Related Topics
In Ruby, What Is the Equivalent to an Interface in C#
Upload File Through C# Using JSON Request and Restsharp
Unity: Apple Game Center iOS7 Registers the Scores for Only 30 Minutes in the Leaderboard
How to Call Code Behind Server Method from a Client Side JavaScript Function
JavaScript Date to C# via Ajax
How to Call an ASP.NET C# Method Using JavaScript
Read and Write File on Streamingassetspath
How to Stream Webcam Video with C#
Copy Files from Resources/Streamingassets to Application.Persistentdatapath Upon Installation
A Method for Making Http Requests on Unity iOS
Simple iOS Bluetooth Data Transmission Using Unity
What Is a Dynamic Language, and Why Doesn't C# Qualify
.Net Core 2.1 Identity Get All Users with Their Associated Roles
Style Bundling Not Working After Iis Deployment (MVC 4)
Is There a Faster Way to Scan Through a Directory Recursively in .Net