WPF C# 获取机器信息

2017年8月5日 0 作者 snowysong@live.com
阅读需要3分钟

 0x00.添加引用

using System; using System.Collections.Specialized; 
using System.IO; 
using System.Management; 
using System.Net; 
using System.Net.Sockets; 
using System.Windows; 
using System.Windows.Input; 
using System.Windows.Media;

0x01.获取机器名称

string machineName = Environment.MachineName;

0x02.获取MAC地址

string result;
try
{using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
using (ManagementObjectCollection moc = mc.GetInstances())
{
string macAddress = "";
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
macAddress = mo["MacAddress"].ToString();
break;
}
}
result = macAddress;
}
}
}
catch
{
result = "unknown";
}
finally
{
}

0x03.判断32/64位系统

string result;
bool is64OS = Environment.Is64BitOperatingSystem;
if (is64OS == true)
result = "64 bits OS";//64位
else
result = "32 bits OS";//32位

0x04.获取硬盘信息

string HDSN = "";
string HDID = "";

ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDSN = (string)mo.Properties["Model"].Value;
}
ManagementClass mc1 = new ManagementClass("Win32_PhysicalMedia");
ManagementObjectCollection moc2 = mc1.GetInstances();

foreach (ManagementObject mo in moc2)
{
HDID = mo.Properties["SerialNumber"].Value.ToString().Trim();
}

0x05.IPV4

string[] GetLocalIpv4()
string Ipv4;
{
IPAddress[] localIPs;
localIPs = Dns.GetHostAddresses(Dns.GetHostName());
StringCollection IpCollection = new StringCollection();
foreach (IPAddress ip in localIPs)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
IpCollection.Add(ip.ToString());
}
string[] IpArray = new string[IpCollection.Count];
IpCollection.CopyTo(IpArray, 0);
return IpArray;
}

void ShowIP()
{
foreach (string ip in GetLocalIpv4())
{
Ipv4 = ip.ToString();
}
return;
}

ShowIP();

0x06.IPV6

string ip =  "";
IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
    ip = addressList[i].ToString();
}

0x07.获取厂商和机器型号(manufacturer)(model)

string manufacturer;
string model;
ManagementClass mc2 = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc3 = mc2.GetInstances();

if (moc3.Count != 0)
{
    foreach (ManagementObject mo in mc2.GetInstances())
    {
        manufacturer =mo["Manufacturer"].ToString();///////////制造厂商
    }
}

foreach (ManagementObject m in moc3)
{
    model = m["model"].ToString ();////机器型号
}

 

0 0 vote
Article Rating