c#如何获得cpu,硬盘的物理序列号
简介:这是c#如何获得cpu,硬盘的物理序列号的详细页面,介绍了和c/c++,有关的知识,加入收藏请按键盘ctrl+D,谢谢大家的观看!要查看更多有关信息
using system;
using system.runtime.interopservices;
using system.management;
namespace hardware
{
/// <summary>
/// hardware_mac 的摘要说明。
/// </summary>
public class hardwareinfo
{
//取机器名
public string gethostname()
{
return system.net.dns.gethostname();
}
//取cpu编号
public string getcpuid()
{
try
{
managementclass mc = new managementclass("win32_processor");
managementobjectcollection moc = mc.getinstances();
string strcpuid = null ;
foreach( managementobject mo in moc )
{
strcpuid = mo.properties["processorid"].value.tostring();
break;
}
return strcpuid;
}
catch
{
return "";
}
}//end method
//获取cpu序列号
public string getcpuid2()
{
try
{
System.Management.ManagementObjectSearcher Wmi = new ManagementObjectSearcher ( "SELECT * FROM Win32_Processor ");
string tmpUint32_1 = string.Empty;
foreach(ManagementObject WmiObj in Wmi.Get())
{
tmpUint32_1 =WmiObj[ "ProcessorId "].ToString();
}
this.textBox2.Text = tmpUint32_1;
this.gProgressBar1.Value ++;
}
catch(Exception ex2)
{
throw new Exception(ex2.ToString());
}
}
//取第一块硬盘编号
public string getharddiskid()
{
try
{
managementobjectsearcher searcher = new managementobjectsearcher("select * from win32_physicalmedia");
string strharddiskid = null ;
foreach(managementobject mo in searcher.get())
{
strharddiskid = mo["serialnumber"].tostring().trim();
break;
}
return strharddiskid ;
}
catch
{
return "";
}
}//end
//获取磁盘序列号
public string getharddiskid2()
{
try
{
System.Management.ManagementObjectSearcher cmicWmi = new ManagementObjectSearcher( "SELECT * FROM Win32_DiskDrive ");
System.UInt32 tmpUint32 = 0;
foreach(ManagementObject cmicWmiObj in cmicWmi.Get())
{
tmpUint32 = Convert.ToUInt32(cmicWmiObj[ "signature "].ToString());
}
this.textBox1.Text = tmpUint32.ToString();
this.gProgressBar1.Value ++;
}
catch(Exception ex1)
{
throw new Exception(ex1.ToString());
}
}
/// <summary>
/// 获取硬盘ID
/// </summary>
/// <returns> string </returns>
public string GetHDid()
{
string HDid = " ";
using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"))
{
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
mo.Dispose();
}
}
return HDid.ToString();
}
/// <summary>
/// 获取网卡硬件地址
/// </summary>
/// <returns> string </returns>
public string GetMoAddress()
{
string MoAddress = " ";
using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if ((bool)mo["IPEnabled"] == true)
MoAddress = mo["MacAddress"].ToString();
mo.Dispose();
}
}
return MoAddress.ToString();
}
}
}