[轉貼] 獲取本機電腦名稱,IP,MAC地址,硬碟ID
出處:http://www.cnblogs.com/qixuejia/archive/2011/01/12/1933442.html
using System;
using System.Management;
public class ComputerInfo
{
public string CpuID;
public string MacAddress;
public string DiskID;
public string IpAddress;
public string LoginUserName;
public string ComputerName;
public string SystemType;
public string TotalPhysicalMemory; //單位:M
private static ComputerInfo _instance;
public static ComputerInfo Instance()
{
if (_instance == null)
_instance = new ComputerInfo();
return _instance;
}
public ComputerInfo()
{
CpuID = GetCpuID();
MacAddress = GetMacAddress();
DiskID = GetDiskID();
IpAddress = GetIPAddress();
LoginUserName = GetUserName();
SystemType = GetSystemType();
TotalPhysicalMemory = GetTotalPhysicalMemory();
ComputerName = GetComputerName();
}
//獲取CPU序列號
public string GetCpuID()
{
try
{
string cpuInfo = "";
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
moc = null;
mc = null;
return cpuInfo;
}
catch
{
return "unknow";
}
finally
{
}
}
//獲取網卡硬件地址
public string GetMacAddress()
{
try
{
string mac = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
mac = mo["MacAddress"].ToString();
break;
}
}
moc = null;
mc = null;
return mac;
}
catch
{
return "unknow";
}
finally
{
}
}
//獲取IP地址
public string GetIPAddress()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
//st=mo["IpAddress"].ToString();
System.Array ar;
ar = (System.Array)(mo.Properties["IpAddress"].Value);
st = ar.GetValue(0).ToString();
break;
}
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
finally
{
}
}
//獲取硬盤ID
public string GetDiskID()
{
try
{
String HDid = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;
}
moc = null;
mc = null;
return HDid;
}
catch
{
return "unknow";
}
finally
{
}
}
//操作系統的登錄用戶名
public string GetUserName()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
st = mo["UserName"].ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
finally
{
}
}
//PC類型
public string GetSystemType()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
st = mo["SystemType"].ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
finally
{
}
}
//物理內存
public string GetTotalPhysicalMemory()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
st = mo["TotalPhysicalMemory"].ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
finally
{
}
}
//電腦名稱
public string GetComputerName()
{
try
{
return System.Environment.GetEnvironmentVariable("ComputerName");
}
catch
{
return "unknow";
}
finally
{
}
}
}
相關文章