using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace GetPCInformation
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p. GetCPUSerialNumber();
p.GetPhy();
// Console.WriteLine(p.pcInformation["memoryModel"]);
p.GetDriverInfo();
p.GetIP();
foreach (var item in p.pcInformation)
{
Console.WriteLine(item.Key+":"+item.Value);
}
Console.Read();
}
Dictionary<string, string> pcInformation = new Dictionary<string, string>();
Dictionary<string, string> driverInformation = new Dictionary<string, string>();
/// <summary>
/// CPU 型号/系统bit/系统名
/// </summary>
public void GetCPUSerialNumber()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Processor");
foreach (ManagementObject mo in searcher.Get())
{
pcInformation.Add("OSName", mo["SystemName"].ToString().Trim());//系统名
pcInformation.Add("CPUName", mo["Name"].ToString().Trim()); //处理器名称
pcInformation.Add("OSBit", mo["AddressWidth"].ToString().Trim());// 系统位数
}
foreach (var o in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get())
{
var obj = (ManagementObject)o;
// Microsoft Windows 11 企业版
var caption = obj.GetPropertyValue("Caption");
// 10.0.22000
var version = obj.GetPropertyValue("Version");
pcInformation.Add("version", caption.ToString());
pcInformation.Add("version2", version.ToString());
break;
}
pcInformation.Add("userName", Environment.UserName);
pcInformation.Add("netWork", SystemInformation.Network ?"已连接":"未连接");
}
catch (Exception e)
{
Console.WriteLine(e.Message); ;
}
}
/// <summary>
/// 获取内存大小/可用内存/已使用内存
/// </summary>
public void GetPhy()
{
try
{
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
double available = 0, capacity = 0;
foreach (ManagementObject mo1 in moc1)
{
capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
}
moc1.Dispose();
cimobject1.Dispose();
//获取内存可用大小
ManagementClass cimobject2 = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory");
ManagementObjectCollection moc2 = cimobject2.GetInstances();
foreach (ManagementObject mo2 in moc2)
{
available += ((Math.Round(Int64.Parse(mo2.Properties["AvailableMBytes"].Value.ToString()) / 1024.0, 1)));
}
moc2.Dispose();
cimobject2.Dispose();
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_PhysicalMemory");
ManagementObjectCollection.ManagementObjectEnumerator moe = mos.Get().GetEnumerator();
moe.MoveNext();
Console.WriteLine();
pcInformation.Add("memoryMfrs", moe.Current.Properties["Manufacturer"].Value.ToString());
pcInformation.Add("memorySum", capacity.ToString().Trim() + "G");
pcInformation.Add("memoryAvailable", available.ToString().Trim() + "G");
pcInformation.Add("memoryUsed", ((capacity - available)).ToString() + "G," + (Math.Round((capacity - available) / capacity * 100, 0)).ToString() + "%");
}
catch (Exception e)
{
Console.WriteLine(e.Message); ;
}
}
/// <summary>
/// 获取硬盘 使用情况
/// </summary>
public void GetDriverInfo() {
List<string> list = new List<string>();
foreach (DriveInfo item in DriveInfo.GetDrives())
{
if (item.DriveType == DriveType.Fixed)
{
long total = item.TotalSize / (1024 * 1024 * 1024);
long free = item.TotalFreeSpace / (1024 * 1024 * 1024);
list.Add(item.Name.ToString() +"," + total.ToString());
driverInformation.Add(item.Name, total.ToString() + "G-" + free.ToString() + "G-" + (1 - Math.Round(free * 1.0 / total, 2)) * 100 + "%");
double rate = Math.Round(free * 1.0 / total,2);
Console.WriteLine(item.Name+total.ToString() + "G-" + free.ToString() + "G-" + (1-Math.Round(free * 1.0 / total, 2))*100 + "%");
}
}
}
/// <summary>
/// 获取IP地址和MAC地址
/// </summary>
public void GetIP()
{
try
{
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
foreach (IPAddress ipa in ipadrlist)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
pcInformation.Add("IP", ipa.ToString());
}
//创建ManagementClass对象
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string macAddress = string.Empty;
foreach (ManagementObject mo in moc)//遍历获取的集合
{
if ((bool)mo["IPEnabled"])//判断IPEnabled的属性是否为true
{
macAddress = mo["MacAddress"].ToString();//获取网卡的序列号
}
}
pcInformation.Add("MAC", macAddress.ToString());
}
catch (Exception e)
{
//这里写异常的处理(最好写入日志文件)
Console.WriteLine(e.Message);
}
}
}
}