我正在C#中开发一个库,该库使用这3个变量生成唯一的硬件ID
- 机器名称
- MAC地址
- 硬盘驱动器序列号
我可以在.NET和Mono中获得机器名称和MAC地址,但我只能在.NET中获得硬盘驱动器序列号。有人知道是否有任何可能的方法可以在Mono中获取硬盘驱动器序列号码,或者我应该只使用另一个变量(即:CPU名称、主板ID等)吗?
根据本文档:
Mac OS X不支持从用户级应用程序获取硬盘序列号
如果在mac上成为root用户的要求对你来说不是问题(或者你跳过了mac版本),我有一个解决问题的方法:
使用这篇文章或这个问题,你可以确定:
- 你运行的是Mono还是.NET
- 你在哪个站台上
如果你知道你在LINUX系统上,你可以通过运行这样的系统命令来获得硬盘串行:
/sbin/udevadm info --query=property --name=sda
在mac上,你可以使用Disk Utility(作为root用户)来获取硬盘驱动器序列号。在windows上,您可以使用标准方法。
您也可以使用ioreg 获得用户权限
来自外壳:
ioreg-p IOService-n AppleAHCIDiskDriver-r | grep \"序列号\"| awk"{print$NF;}"
程序化:
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;
}