C# 确定mono中的硬盘驱动器序列号?
我正在用C#开发一个库,它使用这3个变量生成一个唯一的硬件IDC# 确定mono中的硬盘驱动器序列号?,c#,mono,hardware,uniqueidentifier,serial-number,C#,Mono,Hardware,Uniqueidentifier,Serial Number,我正在用C#开发一个库,它使用这3个变量生成一个唯一的硬件ID 机器名 MAC地址 硬盘驱动器序列号 我可以在.NET和Mono中获取机器名和MAC地址,但只能在.NET中获取硬盘序列号。是否有人知道是否有任何可能的方法在mono中获取硬盘驱动器序列号,或者我应该使用另一个变量(即:CPU名称、主板ID等)?根据: Mac OS X不支持从用户级应用程序获取硬盘序列号 如果在mac上成为root用户的要求对你来说不是问题(或者你跳过了mac版本),我有一个简单的方法来解决这个问题: 使用或回答您
在mac上,您可以使用(作为根用户)获取硬盘驱动器序列号。在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;
}
请注意,机器名和MAC地址都可以手动设置,因此它们不是唯一的。GUID基于MAC地址…并且可以伪造MAC地址:您不能假定它对于特定计算机上的特定以太网接口是唯一的。谢谢!由于MAC OSX的唯一标识符原因,我可能不得不返回到另一个变量。
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;
}