using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management; namespace IPMACDemo
{
class Program
{
static void Main(string[] args)
{ Console.WriteLine("计算机名称:{0}",GetComputerName());
Console.WriteLine("操作系统:{0}", GetSystemType());
Console.WriteLine("用户名:{0}", GetUserName());
Console.WriteLine("计算机MAC地址:{0}", GetMacAddress());
Console.WriteLine("计算机IP地址:{0}", GetIpAddress()); Console.WriteLine("硬盘序列号:{0}", GetDiskSerialNumber());
Console.WriteLine("硬盘大小:{0}", Convert.ToDouble(GetSizeOfDisk()) /(**)); Console.WriteLine("网卡地址:{0}", GetMacAddress());
Console.WriteLine("显卡PNPDeviceID:{0}", GetVideoPnpid());
Console.WriteLine("声卡PNPDeviceID :{0}", GetSoundPnpid()); Console.WriteLine("主板制造商:{0}", GetBoardManufacturer());
Console.WriteLine("主板编号:{0}", GetBoardId());
Console.WriteLine("主板型号:{0}", GetBoardType()); Console.WriteLine("CPU名称:{0}", GetCpuName());
Console.WriteLine("CPU数量:{0}", GetCpuCount());
Console.WriteLine("CPU编号:{0}", GetCpuid());
Console.WriteLine("CPU版本信息:{0}", GetCpuVersion());
Console.WriteLine("CPU制造商:{0}", GetCpuManufacturer()); Console.WriteLine("物理内存:{0}", Convert.ToDouble(GetPhysicalMemory()) / ( * * )); Console.ReadLine(); } #region CPU
/// <summary>
/// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核
/// </summary>
/// <returns></returns>
public static string[] GetCpuMHZ()
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection cpus = mc.GetInstances(); string[] mHz = new string[cpus.Count];
int c = ;
ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
foreach (ManagementObject mo in mySearch.Get())
{
mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
c++;
}
mc.Dispose();
mySearch.Dispose();
return mHz;
} /// <summary>
/// 获取CPU的个数
/// </summary>
/// <returns></returns>
public static int GetCpuCount()
{
try
{
using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
{
ManagementObjectCollection cpus = mCpu.GetInstances();
return cpus.Count;
}
}
catch
{
}
return -;
} /// <summary>
/// 获得CPU编号
/// </summary>
/// <returns></returns>
public static string GetCpuid()
{
var cpuid = string.Empty;
var mc = new ManagementClass("Win32_Processor");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
cpuid = mo.Properties["ProcessorId"].Value.ToString();
}
return cpuid;
} /// <summary>
/// CPU版本信息
/// </summary>
/// <returns></returns>
public static string GetCpuVersion()
{
var st = string.Empty;
var mos = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (var o in mos.Get())
{
var mo = (ManagementObject)o;
st = mo["Version"].ToString();
}
return st;
} /// <summary>
/// CPU名称信息
/// </summary>
/// <returns></returns>
public static string GetCpuName()
{
var st = string.Empty;
var driveId = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (var o in driveId.Get())
{
var mo = (ManagementObject)o;
st = mo["Name"].ToString();
}
return st;
} /// <summary>
/// CPU制造厂商
/// </summary>
/// <returns></returns>
public static string GetCpuManufacturer()
{
var st = string.Empty;
var mos = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (var o in mos.Get())
{
var mo = (ManagementObject)o;
st = mo["Manufacturer"].ToString();
}
return st;
}
#endregion #region 主板
/// <summary>
/// 主板制造厂商
/// </summary>
/// <returns></returns>
public static string GetBoardManufacturer()
{
var query = new SelectQuery("Select * from Win32_BaseBoard");
var mos = new ManagementObjectSearcher(query);
var data = mos.Get().GetEnumerator();
data.MoveNext();
var board = data.Current;
return board.GetPropertyValue("Manufacturer").ToString();
} /// <summary>
/// 主板编号
/// </summary>
/// <returns></returns>
public static string GetBoardId()
{
var st = string.Empty;
var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
foreach (var o in mos.Get())
{
var mo = (ManagementObject)o;
st = mo["SerialNumber"].ToString();
}
return st;
} /// <summary>
/// 主板型号
/// </summary>
/// <returns></returns>
public static string GetBoardType()
{
var st = string.Empty;
var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
foreach (var o in mos.Get())
{
var mo = (ManagementObject)o;
st = mo["Product"].ToString();
}
return st;
} #endregion #region 网卡,声卡,显卡
/// <summary>
/// 获取网卡硬件地址
/// </summary>
/// <returns></returns>
public static string GetMacAddress()
{
var mac = "";
var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
if (!(bool)mo["IPEnabled"]) continue;
mac = mo["MacAddress"].ToString();
break;
}
return mac;
} /// <summary>
/// 显卡PNPDeviceID
/// </summary>
/// <returns></returns>
public static string GetVideoPnpid()
{
var st = "";
var mos = new ManagementObjectSearcher("Select * from Win32_VideoController");
foreach (var o in mos.Get())
{
var mo = (ManagementObject)o;
st = mo["PNPDeviceID"].ToString();
}
return st;
} /// <summary>
/// 声卡PNPDeviceID
/// </summary>
/// <returns></returns>
public static string GetSoundPnpid()
{
var st = string.Empty;
var mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
foreach (var o in mos.Get())
{
var mo = (ManagementObject)o;
st = mo["PNPDeviceID"].ToString();
}
return st;
}
#endregion /// <summary>
/// 获取硬盘序列号
/// </summary>
/// <returns></returns>
public static string GetDiskSerialNumber()
{
//这种模式在插入一个U盘后可能会有不同的结果,如插入我的手机时
var hDid = string.Empty;
var mc = new ManagementClass("Win32_DiskDrive");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
hDid = (string)mo.Properties["Model"].Value;
//这名话解决有多个物理盘时产生的问题,只取第一个物理硬盘
break;
}
return hDid;
} /// <summary>
/// 获取硬盘的大小
/// </summary>
/// <returns></returns>
public static string GetSizeOfDisk()
{
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moj = mc.GetInstances();
foreach (ManagementObject m in moj)
{
return m.Properties["Size"].Value.ToString();
}
return "-1";
} /// <summary>
/// 物理内存
/// </summary>
/// <returns></returns>
public static string GetPhysicalMemory()
{
var st = string.Empty;
var mc = new ManagementClass("Win32_ComputerSystem");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
st = mo["TotalPhysicalMemory"].ToString();
}
return st;
} /// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIpAddress()
{
var st = string.Empty;
var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
if (!(bool)mo["IPEnabled"]) continue;
var ar = (Array)(mo.Properties["IpAddress"].Value);
st = ar.GetValue().ToString();
break;
}
return st;
} /// <summary>
/// 操作系统的登录用户名
/// </summary>
/// <returns></returns>
public static string GetUserName()
{
return Environment.UserName;
} /// <summary>
/// 获取计算机名
/// </summary>
/// <returns></returns>
public static string GetComputerName()
{
return Environment.MachineName;
} /// <summary>
/// 操作系统类型
/// </summary>
/// <returns></returns>
public static string GetSystemType()
{
var st = string.Empty;
var mc = new ManagementClass("Win32_ComputerSystem");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
st = mo["SystemType"].ToString();
}
return st;
}
}
}