How to get MAC ID of a system [duplicate] - c#

This question already has answers here:
Formatting MAC address in C#
(5 answers)
Closed 4 years ago.
I have found this code to get a MAC address, but it returns a long string and doesn't include ':'.
Is it possible to add in the ':' or split up the string and add it it myself?
here is the code:
private object GetMACAddress()
{
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddresses;
}
It returns the value of 00E0EE00EE00 whereas I want it to display something like 00:E0:EE:00:EE:00.
Any ideas?
Thanks.

I'm using following code to access mac address in format you want :
public string GetSystemMACID()
{
string systemName = System.Windows.Forms.SystemInformation.ComputerName;
try
{
ManagementScope theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
ObjectQuery theQuery = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
ManagementObjectCollection theCollectionOfResults = theSearcher.Get();
foreach (ManagementObject theCurrentObject in theCollectionOfResults)
{
if (theCurrentObject["MACAddress"] != null)
{
string macAdd = theCurrentObject["MACAddress"].ToString();
return macAdd.Replace(':', '-');
}
}
}
catch (ManagementException e)
{
}
catch (System.UnauthorizedAccessException e)
{
}
return string.Empty;
}
Or You can use Join method, like this :
return string.Join (":", (from z in nic.GetPhysicalAddress().GetAddressBytes() select z.ToString ("X2")).ToArray());

using System;
using System.Text;
class Program
{
static void Main()
{
Console.WriteLine(MACify("00E0EE00EE00"));
}
static string MACify(string input)
{
var builder = new StringBuilder(input);
for(int i=builder.Length-2; i>0; i-=2)
{
builder.Insert(i,':');
}
return builder.ToString();
}
}
Outputs:
00:E0:EE:00:EE:00

Related

How to list of ports and Mac addresses to connected switch (Cisco) C#

Hello everyone I want to ask about how can list ports and mac addresses of devices connected to switch using C#.
I have found code which works for Dell switch. I need it for Cisco switch
using SnmpSharpNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PortMapper
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<KeyValuePair<string, string>> portList = new List<KeyValuePair<string, string>>();
List<Portmap> portMaps = new List<Portmap>();
public class Portmap
{
public string Hostname { get; set; }
public string Port { get; set; }
public string IP { get; set; }
public string MAC { get; set; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
IPAddress ip = IPAddress.Parse("192.168.0.2");
IPAddress ip2 = IPAddress.Parse("192.168.0.3");
SnmpWalk(ip, "community", "1.3.6.1.2.1.17.7.1.2.2.1.2.1", "1");
SnmpWalk(ip2, "community","1.3.6.1.2.1.17.7.1.2.2.1.2.1", "2");
DhcpQuery("netsh", "dhcp server \\\\servername scope 192.168.0.0 show clients", "192");
List<Portmap> gridResults = new List<Portmap>();
//Example of filtering uplink ports from other switches
foreach(Portmap portMap in portMaps)
{
if (portMap.Port != "1/2/48" && portMap.Port != "2/1/48")
{
gridResults.Add(portMap);
}
}
PortMapGrid.ItemsSource = gridResults;
}
//Use NETSH to retrieve a list of DHCP MAC and IP addresses
private void DhcpQuery(string cmd, string args, string subnet)
{
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.FileName = cmd;
procStartInfo.Arguments = args;
procStartInfo.CreateNoWindow = true;
string output;
using (Process proc = Process.Start(procStartInfo))
{
output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
}
//Find valid leases in command output
string[] lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
List<string> leases = new List<string>();
foreach (string line in lines)
{
if (line.StartsWith(subnet))
{
leases.Add(line);
}
}
//Create threads
Thread[] threadArray = new Thread[leases.Count];
int threadcount = 0;
//Loop each Dhcp Lease
foreach (string line in leases)
{
string[] pieces = line.Split('-');
string ipAddress = pieces[0].Trim();
string mac = "";
string hostname = "";
foreach (string piece in pieces)
{
if (piece.Trim().Length == 2)
{
mac += piece;
}
}
ThreadStart start = delegate
{
hostname = GetHost(ipAddress);
foreach (KeyValuePair<string, string> port in portList)
{
if (port.Key.ToUpper().Trim() == mac.ToUpper().Trim())
{
Portmap portMap = new Portmap();
portMap.IP = ipAddress;
portMap.MAC = mac.ToUpper();
portMap.Port = port.Value;
portMap.Hostname = hostname;
portMaps.Add(portMap);
}
}
};
threadArray[threadcount] = new Thread(start);
threadArray[threadcount].Start();
threadcount = threadcount + 1;
}
//Join all threads in the array to wait for results
for (int i = 0; i < threadcount; i++)
{
threadArray[i].Join();
}
}
//SNMPWALK the ports on a switch or stack of switches. Ports will be labeled SwitchNum/Stack Number/Port Numbers.
private void SnmpWalk(IPAddress ip, string snmpCommunity, string oid, string switchNum)
{
UdpTarget target = new UdpTarget(ip);
// SNMP community name
OctetString community = new OctetString(snmpCommunity);
// Define agent parameters class
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 1
param.Version = SnmpVersion.Ver1;
// Define Oid that is the root of the MIB tree you wish to retrieve
Oid rootOid = new Oid(oid);
// This Oid represents last Oid returned by the SNMP agent
Oid lastOid = (Oid)rootOid.Clone();
// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.GetNext);
// Loop through results
while (lastOid != null)
{
// When Pdu class is first constructed, RequestId is set to a random value
// that needs to be incremented on subsequent requests made using the
// same instance of the Pdu class.
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
}
// Clear Oids from the Pdu class.
pdu.VbList.Clear();
// Initialize request PDU with the last retrieved Oid
pdu.VbList.Add(lastOid);
// Make SNMP request
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
// You should catch exceptions in the Request if using in real application.
// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
lastOid = null;
break;
}
else
{
// Walk through returned variable bindings
foreach (Vb v in result.Pdu.VbList)
{
// Check that retrieved Oid is "child" of the root OID
if (rootOid.IsRootOf(v.Oid))
{
//Convert OID to MAC
string[] macs = v.Oid.ToString().Split('.');
string mac = "";
int counter = 0;
foreach (string chunk in macs)
{
if (counter >= macs.Length - 6)
{
mac += string.Format("{0:X2}", int.Parse(chunk));
}
counter += 1;
}
//Assumes a 48 port switch (52 actual). You need to know these values to correctly iterate through a stack of switches.
int dellSwitch = 1 + int.Parse(v.Value.ToString()) / 52;
int port = int.Parse(v.Value.ToString()) - (52 * (dellSwitch - 1));
KeyValuePair<string, string> Port = new KeyValuePair<string, string>(mac, switchNum + "/" + dellSwitch.ToString() + "/" + port.ToString());
portList.Add(Port);
//Exit Loop
lastOid = v.Oid;
}
else
{
//End of the requested MIB tree. Set lastOid to null and exit loop
lastOid = null;
}
}
}
}
else
{
Console.WriteLine("No response received from SNMP agent.");
}
}
target.Close();
}
private string GetHost(string ipAddress)
{
try
{
IPHostEntry entry = Dns.GetHostEntry(ipAddress);
return entry.HostName;
}
catch
{
return "";
}
}
}
}
Can Anyone is going to help me out for finding the code or give me a way that how can I convert this code to work for Cisco. what Oid it can be?

C#: Arduino not recognized after Windows 10 upgrade

Please check the code that I am using:
public static string AutoDetectArduinoPort()
{
ConnectionOptions options = PrepareOptions();
ManagementScope scope = PrepareScope(Environment.MachineName, options, #"\root\CIMV2");
// Prepare the query and searcher objects.
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
ManagementObjectSearcher portSearcher = new ManagementObjectSearcher(scope, objectQuery);
using (portSearcher)
{
string portName = "";
foreach (ManagementObject currentObject in portSearcher.Get())
{
if (currentObject != null)
{
object currentObjectCaption = currentObject["Caption"];
if (currentObjectCaption != null)
{
portName = currentObjectCaption.ToString();
if (portName.Contains("(COM"))
{
if (portName.Contains("Arduino")) // e.g. "Arduino Mega 2560 (COM3)"
{
portName = portName.Substring(portName.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty);
return portName;
}
}
}
}
}
}
return null;
}
And i call it like this:
string portName = AutoDetectArduinoPort();
MessageBox.Show(portName); // shows "COM3"
if (portName != null)
{
serialPort.PortName = portName;
serialPort.Open();
......
Runtime exception (System.IO.IOException) is thrown at this last line saying that "The port 'COM3' does not exist." while it is showing in Device Manager and all.
What am i doing wrong? Please help me fix this prob. Irony is that it was working all fine before windows 10 upgrade :(

Identify COM port using VID and PID for USB device attached to x64

As following i able to get usb com port names attached to 32bit win7OS machine, by given pid and vid,but when running in x64 it stuck in the following line:
comports.Add((string)rk6.GetValue("PortName"));
This is my code
static List<string> ComPortNames(String VID, String PID)
{
String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
List<string> comports = new List<string>();
RegistryKey rk1 = Registry.LocalMachine;
RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");
foreach (String s3 in rk2.GetSubKeyNames())
{
RegistryKey rk3 = rk2.OpenSubKey(s3);
foreach (String s in rk3.GetSubKeyNames())
{
if (_rx.Match(s).Success)
{
RegistryKey rk4 = rk3.OpenSubKey(s);
foreach (String s2 in rk4.GetSubKeyNames())
{
RegistryKey rk5 = rk4.OpenSubKey(s2);
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
comports.Add((string)rk6.GetValue("PortName"));
}
}
}
}
return comports;
}
actual code get here, So how to get com port names in x64, any suggestion?
While I was testing the answer from Youkko under Windows 10 x64 I was getting some strange results and looking at the registry on my machine the LocationInformation keys contained strings such as Port_#0002.Hub_#0003so they are related to the USB hub / port the device is connected to not the COM port allocated by Windows.
So in my case I was getting COM2 included which is a hardware port on my motherboard and it skipped the COM5 port I was expecting but that was located under the PortName registry key. I'm not sure if something has changed since the version of Windows you were using but I think your main problem might have been not checking for null values on the keys.
The following slightly modified version seems to work fine on a variety or Windows 7 / 10 and x32 / 64 systems and I've also added a to check of SerialPort.GetPortNames() to make sure the device is available and plugged into the system before returning it:
static List<string> ComPortNames(String VID, String PID)
{
String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
List<string> comports = new List<string>();
RegistryKey rk1 = Registry.LocalMachine;
RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");
foreach (String s3 in rk2.GetSubKeyNames())
{
RegistryKey rk3 = rk2.OpenSubKey(s3);
foreach (String s in rk3.GetSubKeyNames())
{
if (_rx.Match(s).Success)
{
RegistryKey rk4 = rk3.OpenSubKey(s);
foreach (String s2 in rk4.GetSubKeyNames())
{
RegistryKey rk5 = rk4.OpenSubKey(s2);
string location = (string)rk5.GetValue("LocationInformation");
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
string portName = (string)rk6.GetValue("PortName");
if (!String.IsNullOrEmpty(portName) && SerialPort.GetPortNames().Contains(portName))
comports.Add((string)rk6.GetValue("PortName"));
}
}
}
}
return comports;
}
By reading your code, I found out that the current path you're looking at in registry does not contain any information about ports.
But I found a way to read it by doing this little change:
static List<string> ComPortNames(String VID, String PID)
{
String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
List<string> comports = new List<string>();
RegistryKey rk1 = Registry.LocalMachine;
RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");
foreach (String s3 in rk2.GetSubKeyNames())
{
RegistryKey rk3 = rk2.OpenSubKey(s3);
foreach (String s in rk3.GetSubKeyNames())
{
if (_rx.Match(s).Success)
{
RegistryKey rk4 = rk3.OpenSubKey(s);
foreach (String s2 in rk4.GetSubKeyNames())
{
RegistryKey rk5 = rk4.OpenSubKey(s2);
string location = (string)rk5.GetValue("LocationInformation");
if (!String.IsNullOrEmpty(location))
{
string port = location.Substring(location.IndexOf('#') + 1, 4).TrimStart('0');
if (!String.IsNullOrEmpty(port)) comports.Add(String.Format("COM{0:####}", port));
}
//RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
//comports.Add((string)rk6.GetValue("PortName"));
}
}
}
}
return comports;
}
It did work perfectly.
Thank you for your code, by the way... It helped me a lot!
Here is my take on this (even if it is not a direct A to the Q)
USB devices is always (and has always) enumerated under HKLM\SYSTEM\CurrentControlSet\Enum\USB (note USB at the end)
Device nodes have the format VID_xxxx&PID_xxxx* where xxxx is hexadecimal, there might be some extra function data at the end
Each device node has a sub identifier node based on serialnumber or other data of the device and functions
identifier node can have value "FriendlyName" which some times have the COM in parantheses such as "Virtual Serial Port (COM6)"
Resulting path: HKLM\SYSTEM\CurrentControlSet\Enum\USB\VID_xxxx&PID_xxxx*\*\Device Parameters\ has value named "PortName"
Currently available com ports is listed by System.IO.Ports.SerialPort.GetPortNames()
OpenSubKey implements IDisposable and should have using or .Dispose() on them
using System.IO.Ports;
using System.Linq;
using Microsoft.Win32;
public class UsbSerialPort
{
public readonly string PortName;
public readonly string DeviceId;
public readonly string FriendlyName;
private UsbSerialPort(string name, string id, string friendly)
{
PortName = name;
DeviceId = id;
FriendlyName = friendly;
}
private static IEnumerable<RegistryKey> GetSubKeys(RegistryKey key)
{
foreach (string keyName in key.GetSubKeyNames())
using (var subKey = key.OpenSubKey(keyName))
yield return subKey;
}
private static string GetName(RegistryKey key)
{
string name = key.Name;
int idx;
return (idx = name.LastIndexOf('\\')) == -1 ?
name : name.Substring(idx + 1);
}
public static IEnumerable<UsbSerialPort> GetPorts()
{
var existingPorts = SerialPort.GetPortNames();
using (var enumUsbKey = Registry.LocalMachine.OpenSubKey(#"SYSTEM\CurrentControlSet\Enum\USB"))
{
if (enumUsbKey == null)
throw new ArgumentNullException("USB", "No enumerable USB devices found in registry");
foreach (var devBaseKey in GetSubKeys(enumUsbKey))
{
foreach (var devFnKey in GetSubKeys(devBaseKey))
{
string friendlyName =
(string) devFnKey.GetValue("FriendlyName") ??
(string) devFnKey.GetValue("DeviceDesc");
using (var devParamsKey = devFnKey.OpenSubKey("Device Parameters"))
{
string portName = (string) devParamsKey?.GetValue("PortName");
if (!string.IsNullOrEmpty(portName) &&
existingPorts.Contains(portName))
yield return new UsbSerialPort(portName, GetName(devBaseKey) + #"\" + GetName(devFnKey), friendlyName);
}
}
}
}
}
public override string ToString()
{
return string.Format("{0} Friendly: {1} DeviceId: {2}", PortName, FriendlyName, DeviceId);
}
}
Ok, using ManagementObjectSearcher (it gives COM-port index and VID and PID if they exist):
List < List <string>> USBCOMlist = new List<List<string>>();
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["Caption"].ToString().Contains("(COM"))
{
List<string> DevInfo = new List<string>();
string Caption = queryObj["Caption"].ToString();
int CaptionIndex = Caption.IndexOf("(COM");
string CaptionInfo = Caption.Substring(CaptionIndex + 1).TrimEnd(')'); // make the trimming more correct
DevInfo.Add(CaptionInfo);
string deviceId = queryObj["deviceid"].ToString(); //"DeviceID"
int vidIndex = deviceId.IndexOf("VID_");
int pidIndex = deviceId.IndexOf("PID_");
string vid = "", pid = "";
if (vidIndex != -1 && pidIndex != -1)
{
string startingAtVid = deviceId.Substring(vidIndex + 4); // + 4 to remove "VID_"
vid = startingAtVid.Substring(0, 4); // vid is four characters long
//Console.WriteLine("VID: " + vid);
string startingAtPid = deviceId.Substring(pidIndex + 4); // + 4 to remove "PID_"
pid = startingAtPid.Substring(0, 4); // pid is four characters long
}
DevInfo.Add(vid);
DevInfo.Add(pid);
USBCOMlist.Add(DevInfo);
}
}
}
catch (ManagementException e)
{
MessageBox.Show(e.Message);
}
I think ManagementObjectSearcher may be a better approach than directly reading the registry.
Here's an example for virtual COM ports.

Get user login name in C#

How to check login user name from the system in c#
I tried it using this method
static string whoisLoggedIn(string HostOrIP)
{
GUFlag = true;
HostOrIP = Environment.MachineName;
System.Management.ConnectionOptions myConnectionOptions = new System.Management.ConnectionOptions();
myConnectionOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate;
System.Management.ManagementScope objwmiservice;
System.Management.ManagementObjectSearcher myObjectSearcher2;
System.Management.ManagementObjectCollection myCollection2;
try
{
objwmiservice = new System.Management.ManagementScope(("\\\\" + (HostOrIP +
"\\root\\cimv2")), myConnectionOptions);
objwmiservice.Connect();
myObjectSearcher2 = new System.Management.ManagementObjectSearcher(objwmiservice.Path.ToString(),
"Select UserName from Win32_ComputerSystem");
myObjectSearcher2.Options.Timeout = new TimeSpan(0, 0, 0, 0, 7000);
myCollection2 = myObjectSearcher2.Get();
GUFlag = false;
foreach (System.Management.ManagementObject myObject in myCollection2)
{
if (!(myObject.GetPropertyValue("Username") == null))
{
string Userx = myObject.GetPropertyValue("Username").ToString();
int posx = Userx.LastIndexOf("\\");
if ((posx > 0))
{
Userx = Userx.Substring((posx + 1));
return Userx.ToUpper();
}
}
}
return "<Nobody>";
}
catch (Exception)
{
return "<Nobody>";
}
finally {
GUFlag = false;
}
}
But the problem is some time deadlock occur on myObjectSearcher2.Get();
Is there any way available to get login username
did you try that?
Environment.UserName
it will give you the user name of the user currently login on windows
EDIT
I found this bit of code here http://www.debugging.com/bug/20243, it may solve your issue.
solution by using WMI ( http://msdn.microsoft.com/en-us/library/system.management.aspx ):
private string GetUserName()
{
string result = "";
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName, Name FROM Win32_ComputerSystem"))
{
foreach (ManagementObject mo in searcher.Get())
{
if (mo["UserName"] != null)
result = mo["UserName"].ToString();
if (mo["Name"] != null)
result += " (" + mo["Name"].ToString() + ")";
}
}
return result;
}
Unless I'm not understanding you correctly, I believe it's just:
using System.Security.Principal;
this.nametext = WindowsIdentity.GetCurrent().Name;

How to fast get Hardware-ID in C#?

I need in my program to tie a license to a hardware ID. I tried use WMI, but it still slow.
I need, for example, CPU, HDD, and motherboard info.
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.
I got here looking for the same thing and I found another solution. If you guys are interested I share this class:
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
}
}
I won't take any credit for this because I found it here
It worked faster than I expected for me. Without the graphic card, mac and drive id's I got the unique ID in about 2-3 seconds. With those above included I got it in about 4-5 seconds.
Note: Add reference to System.Management.
The following approach was inspired by this answer to a related (more general) question.
The approach is to read the MachineGuid value in registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography. This value is generated during OS installation.
There are few ways around the uniqueness of the Hardware-ID per machine using this approach. One method is editing the registry value, but this would cause complications on the user's machine afterwards. Another method is to clone a drive image which would copy the MachineGuid value.
However, no approach is hack-proof and this will certainly be good enough for normal users. On the plus side, this approach is quick performance-wise and simple to implement.
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();
}
}
}
We use a combination of the processor id number (ProcessorID) from Win32_processor and the universally unique identifier (UUID) from Win32_ComputerSystemProduct:
ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
mbsList = mos.Get();
string processorId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
processorId = mo["ProcessorID"] as string;
}
mos = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct");
mbsList = mos.Get();
string systemId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
systemId = mo["UUID"] as string;
}
var compIdStr = $"{processorId}{systemId}";
Previously, we used a combination: processor ID ("Select ProcessorID From Win32_processor") and the motherboard serial number ("SELECT SerialNumber FROM Win32_BaseBoard"), but then we found out that the serial number of the motherboard may not be filled in, or it may be filled in with uniform values:
To be filled by O.E.M.
None
Default string
Therefore, it is worth considering this situation.
Also keep in mind that the ProcessorID number may be the same on different computers.
I have refactored Alex Sutu approach to be faster and simpler code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Security.Cryptography;
namespace Test
{
/// <summary>
/// Generates a Guid based on the current computer hardware
/// Example: C384B159-8E36-6C85-8ED8-6897486500FF
/// </summary>
public class SystemGuid
{
private static string _systemGuid = string.Empty;
public static string Value()
{
if (string.IsNullOrEmpty(_systemGuid))
{
var lCpuId = GetCpuId();
var lBiodId = GetBiosId();
var lMainboard = GetMainboardId();
var lGpuId = GetGpuId();
var lMac = GetMac();
var lConcatStr = $"CPU: {lCpuId}\nBIOS:{lBiodId}\nMainboard: {lMainboard}\nGPU: {lGpuId}\nMAC: {lMac}";
_systemGuid = GetHash(lConcatStr);
}
return _systemGuid;
}
private static string GetHash(string s)
{
try
{
var lProvider = new MD5CryptoServiceProvider();
var lUtf8 = lProvider.ComputeHash(ASCIIEncoding.UTF8.GetBytes(s));
return new Guid(lUtf8).ToString().ToUpper();
}
catch (Exception lEx)
{
return lEx.Message;
}
}
#region Original Device ID Getting Code
//Return a hardware identifier
private static string GetIdentifier(string pWmiClass, List<string> pProperties)
{
string lResult = string.Empty;
try
{
foreach (ManagementObject lItem in new ManagementClass(pWmiClass).GetInstances())
{
foreach (var lProperty in pProperties)
{
try
{
switch(lProperty)
{
case "MACAddress":
if (string.IsNullOrWhiteSpace(lResult) == false)
return lResult; //Return just the first MAC
if (lItem["IPEnabled"].ToString() != "True")
continue;
break;
}
var lItemProperty = lItem[lProperty];
if (lItemProperty == null)
continue;
var lValue = lItemProperty.ToString();
if (string.IsNullOrWhiteSpace(lValue) == false)
lResult += $"{lValue}; ";
}
catch { }
}
}
}
catch{}
return lResult.TrimEnd(' ', ';');
}
private static List<string> ListOfCpuProperties = new List<string>{ "UniqueId", "ProcessorId", "Name", "Manufacturer" };
private static string GetCpuId()
{
return GetIdentifier("Win32_Processor", ListOfCpuProperties);
}
private static List<string> ListOfBiosProperties = new List<string> { "Manufacturer", "SMBIOSBIOSVersion", "IdentificationCode", "SerialNumber", "ReleaseDate", "Version" };
//BIOS Identifier
private static string GetBiosId()
{
return GetIdentifier("Win32_BIOS", ListOfBiosProperties);
}
private static List<string> ListOfMainboardProperties = new List<string> { "Model", "Manufacturer", "Name", "SerialNumber" };
//Motherboard ID
private static string GetMainboardId()
{
return GetIdentifier("Win32_BaseBoard", ListOfMainboardProperties);
}
private static List<string> ListOfGpuProperties = new List<string> { "Name" };
//Primary video controller ID
private static string GetGpuId()
{
return GetIdentifier("Win32_VideoController", ListOfGpuProperties);
}
private static List<string> ListOfNetworkProperties = new List<string> { "MACAddress" };
private static string GetMac()
{
return GetIdentifier("Win32_NetworkAdapterConfiguration", ListOfNetworkProperties);
}
#endregion
}
}
Here is a DLL that shows:
* Hard drive ID (unique hardware serial number written in drive's IDE electronic chip)
* Partition ID (volume serial number)
* CPU ID (unique hardware ID)
* CPU vendor
* CPU running speed
* CPU theoretic speed
* Memory Load ( Total memory used in percentage (%) )
* Total Physical ( Total physical memory in bytes )
* Avail Physical ( Physical memory left in bytes )
* Total PageFile ( Total page file in bytes )
* Available PageFile( Page file left in bytes )
* Total Virtual( Total virtual memory in bytes )
* Available Virtual ( Virtual memory left in bytes )
* Bios unique identification numberBiosDate
* Bios unique identification numberBiosVersion
* Bios unique identification numberBiosProductID
* Bios unique identification numberBiosVideo
(text grabbed from original web site)
It works with C#.

Categories