Determine hard drive serial number in mono?
-
13-11-2019 - |
Pergunta
I am developing a library in C# that generates a unique hardware ID using these 3 variables
- Machine name
- MAC address
- Hard drive serial number
I am able to get the machine name and MAC address in both .NET and Mono but I can only get the hard drive serial number in .NET. Does anyone know if there is any possible way to get the hard drive serial number in mono or should I just use another variable (ie: CPU name, Motherboard ID, etc)?
Solução
According to this documentation:
Mac OS X doesn't support getting hard disk serial number from user-level application
If the requirement to be root on mac is not a problem for you (or you skip the mac version), I have one brute way to solve the problem:
Using this article or this question you can determine:
- Are you running Mono or .NET
- Which platform are you on
If you know you are on the LINUX system, you can get hardrive serial through running such system command:
/sbin/udevadm info --query=property --name=sda
On mac you can use Disk Utility (as root) to get harddrive serial. On windows you can use the standard approach.
Outras dicas
You can get it also with user permission with ioreg
from a shell:
ioreg -p IOService -n AppleAHCIDiskDriver -r|grep \"Serial Number\"|awk '{print $NF;}'
programmatically:
uint GetVolumeSerial(string rootPathName)
{
uint volumeSerialNumber = 0;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/usr/sbin/ioreg";
psi.UseShellExecute = false;
psi.Arguments = "-p IOService -n AppleAHCIDiskDriver -r -d 1";
psi.RedirectStandardOutput = true;
Process p = Process.Start(psi);
string output;
do
{
output = p.StandardOutput.ReadLine();
int idx = output.IndexOf("Serial Number");
if (idx != -1)
{
int last = output.LastIndexOf('"');
int first = output.LastIndexOf('"', last - 1);
string tmp = output.Substring(first + 1, last - first - 1);
volumeSerialNumber = UInt32.Parse(tmp);
break;
}
} while (!p.StandardOutput.EndOfStream);
p.WaitForExit();
p.Close();
return volumeSerialNumber;
}