代码之家  ›  专栏  ›  技术社区  ›  Bastianon Massimo

如何使硬盘序列号与linux/windows兼容

  •  2
  • Bastianon Massimo  · 技术社区  · 7 年前

    有没有办法独立于操作系统获取主硬盘序列号? 在windows中使用WMI或DllImport很容易,但是我找不到任何关于linux的文档。。 或者,是否有方法获得跨平台唯一的机器id? P、 我不在乎虚拟机器克隆

    2 回复  |  直到 7 年前
        1
  •  2
  •   Derviş Kayımbaşıoğlu    7 年前

    在linux终端中,您可以使用下面的命令来获取HDD的序列号。

    udevadm info --query=all --name=/dev/sda | grep ID_SERIAL 
    

    哪里 /dev/sda 通常是主硬盘驱动器。

    public static string Bash(this string cmd)
    {
        var escapedArgs = cmd.Replace("\"", "\\\"");
    
        var process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/bash",
                Arguments = $"-c \"{escapedArgs}\"",
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true,
            }
        };
        process.Start();
        string result = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        return result;
    }
    

    然后执行

    var output = "udevadm info --query=all --name=/dev/sda | grep ID_SERIAL".Bash();
    
        2
  •  1
  •   Gunjan Raval    7 年前

    试试这个:

    udevadm info --query=all --name=/dev/sda | grep ID_SERIAL

    推荐文章