关闭

获取硬件的ID

1030人阅读 评论(0) 收藏 举报

using System.Management;

 

//取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 "";
}
}

//取第一块硬盘号
public string GetHardDiskID()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
string strHardDiskID = null ;
foreach(ManagementObject mo in searcher.Get())
{   
strHardDiskID = mo["ModuleNumber"].ToString().Trim();
if(strHardDiskID != "")
{
break;
}
}
return strHardDiskID ;
}
catch
{
return "";
}
}//end

//取第一块网卡号
public string GetNetworkID()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
string strNetworkID = null ;
foreach(ManagementObject mo in searcher.Get())
{   
strNetworkID = mo["MACAddress"].ToString().Trim();
if(strNetworkID != "")
{
break;         
}
}
return strNetworkID ;
}
catch
{
return "";
}
}//end

//取串口
public int getport(int portnum)
{
//链接服务器
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
//检测是否已存在相同串口的站点,并获取PORT值
foreach(DirectoryEntry e in root.Children)
{
if(e.SchemaClassName == "IIsWebServer")
{
if(e.Properties["ServerBindings"].Value != null)
{
int strstart = e.Properties["ServerBindings"].Value.ToString().IndexOf(":",0);
int strend = e.Properties["ServerBindings"].Value.ToString().IndexOf(":",strstart+1);
string thisport = e.Properties["ServerBindings"].Value.ToString().Substring((strstart+1),(strend-strstart-1));
if(thisport == portnum.ToString())
{
portnum++;
portnum = getport(portnum);
break;
}
}
}
}
return portnum;
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:789077次
    • 积分:10561
    • 等级:
    • 排名:第1553名
    • 原创:274篇
    • 转载:94篇
    • 译文:0篇
    • 评论:145条
    最新评论
    友情链接