ITPub博客

首页 > 应用开发 > IT综合 > C# 获得CPU、硬盘、mac地址

C# 获得CPU、硬盘、mac地址

原创 IT综合 作者:mrpxq 时间:2007-08-27 20:10:51 0 删除 编辑

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace HardDiskCPUNetInfo
{
class HardInfo
{

public string GetCPUSerialNo()
{
string cpuInfo = "";//cpu序列号
try
{

ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString() + "-";
//MessageBox.Show("cpu序列号:" + cpuInfo.ToString());
//cpuInfo += cpuInfo.ToString() + "-";
}
cpuInfo = cpuInfo.Substring(0, cpuInfo.Length - 1);
return cpuInfo;
}
catch (Exception ex)
{
return "";
}
}


public string GetHardDiskInfo()
{
String HDid ="";
try
{

ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDid += (string)mo.Properties["Model"].Value + "-";
//MessageBox.Show("硬盘序列号:" + HDid.ToString());

}
HDid = HDid.Substring(0, HDid.Length - 1);
return HDid;
}
catch (Exception ex)
{
return "";
}
}

public string GetNetMacAddress()
{
string sNetAddress ="";
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if ((bool)mo["IPEnabled"] == true)
// MessageBox.Show("MAC address {0}" + mo["MacAddress"].ToString());
sNetAddress += mo["MacAddress"].ToString() + "-";
mo.Dispose();
}
sNetAddress = sNetAddress.Substring(0, sNetAddress.Length - 1);
return sNetAddress;

}
catch (Exception ex)
{
return "";
}
}
}
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/490225/viewspace-966398/,如需转载,请注明出处,否则将追究法律责任。

上一篇: C# 操作文件
下一篇: db2 基础
请登录后发表评论 登录
全部评论
  • 博文量
    4
  • 访问量
    33094