C#获取系统硬盘唯一ID,排除U盘

    公司项目要在一个平板上面运行一个软件,然后根据平板的硬盘生成一个唯一ID。因为平板就一个C盘,有的时候插了U盘之后获取的硬盘号会变。在网上找到一个方法,可以识别系统盘的硬盘ID。分享给大家。下面是代码:

public static string GetSystemDiskNo()
        {
            ManagementClass cimObject = new ManagementClass("Win32_PhysicalMedia");
            ManagementObjectCollection moc = cimObject.GetInstances();
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (ManagementObject mo in moc)
            {
                string tag = mo.Properties["Tag"].Value.ToString().ToLower().Trim();
                string hdId = (string)mo.Properties["SerialNumber"].Value ?? string.Empty;
                hdId = hdId.Trim();
                dict.Add(tag, hdId);
            }
            cimObject = new ManagementClass("Win32_OperatingSystem");
            moc = cimObject.GetInstances();
            string currentSysRunDisk = string.Empty;
            foreach (ManagementObject mo in moc)
            {
                currentSysRunDisk = Regex.Match(mo.Properties["Name"].Value.ToString().ToLower(), @"harddisk\d+").Value;
            }
            var results = dict.Where(x => Regex.IsMatch(x.Key, @"physicaldrive" + Regex.Match(currentSysRunDisk, @"\d+$").Value));
            if (results.Any()) return results.ElementAt(0).Value;
            return "";
        }

转自: http://bbs.csdn.net/topics/392006462

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值