c#获取系统所在硬盘的序列号,需要的拿走,不客气

ijunxiong 2016-08-19 05:51:31
原问:http://zhidao.baidu.com/question/329940952509262445.html

//获取系统所在硬盘的序列号
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 "";
}
...全文
774 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
linlin_Money 2017-08-03
  • 打赏
  • 举报
回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧