【问题标题】:Determine hard drive serial number in mono?在单声道中确定硬盘驱动器序列号?
【发布时间】:2023-03-31 04:14:02
【问题描述】:

我正在用 C# 开发一个使用这 3 个变量生成唯一硬件 ID 的库

  1. 机器名称
  2. MAC 地址
  3. 硬盘序列号

我可以在 .NET 和 Mono 中获取机器名称和 MAC 地址,但我只能在 .NET 中获取硬盘序列号。有谁知道是否有任何可能的方法来获取单声道的硬盘序列号,还是我应该只使用另一个变量(即:CPU 名称、主板 ID 等)?

【问题讨论】:

  • 注意,机器名和MAC地址都可以手动设置,所以并不是真正唯一的。
  • GUID 基于 MAC 地址...
  • 而且 MAC 地址可以被欺骗:osxdaily.com/2008/01/17/… 您不能假设它是特定计算机上特定以太网接口的唯一地址。

标签: c# mono hardware uniqueidentifier serial-number


【解决方案1】:

根据this documentation

Mac OS X 不支持从用户级应用程序获取硬盘序列号

如果在 mac 上成为 root 的要求对你来说不是问题(或者你跳过 mac 版本),我有一种解决问题的蛮力方法:

使用this articlethis 可以确定的问题:

  1. 您运行的是 Mono 还是 .NET
  2. 你在哪个平台上

如果你知道你在LINUX系统上,你可以通过running这样system command获得硬盘串口:

/sbin/udevadm info --query=property --name=sda

在 mac 上,您可以使用 Disk Utility(以 root 身份)获取硬盘序列号。在 Windows 上,您可以使用标准方法。

【讨论】:

  • 谢谢!由于 MAC OSX 的唯一标识符原因,我可能不得不恢复为不同的变量。
【解决方案2】:

您也可以通过 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;
    }

【讨论】:

    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2015-07-05
    • 2012-03-29
    • 2014-10-11
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多