引用命名空间
using System.Management;
using System;
using System.Diagnostics;
并在引用中添加
写好的一些方法
/// <summary>
/// CPU名称信息
/// </summary>
/// <returns></returns>
public static string GetCpuName()
{
var CPUName = "";
var management = new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (var baseObject in management.Get())
{
var managementObject = (ManagementObject)baseObject;
CPUName = managementObject["Name"].ToString();
}
return CPUName;
}
/// <summary>
/// CPU制造厂商
/// </summary>
/// <returns></returns>
public static string GetCpuManufacturer()
{
var CPUMakerStr = "";
var management= new ManagementObjectSearcher("Select * from Win32_Processor");
foreach (var baseObject in management.Get())
{
var managementObject = (ManagementObject)baseObject;
CPUMakerStr = managementObject["Manufacturer"].ToString();
}
return CPUMakerStr;
}
using System.Net;
using System.Net.Sockets;
/// <summary>
/// 获取IPv4地址
/// </summary>
/// <returns>IP v4地址</returns>
private static string GetIp1()
{
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
foreach (IPAddress ipa in ipadrlist)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
{
return ipa.ToString();
}
}
return "IPv4为空";
}
/// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIpAddress()
{
var IPv4Str = "";
var management = new ManagementClass("Win32_NetworkAdapterConfiguration");
var managementObject = management.GetInstances();
foreach (ManagementBaseObject managementBaseObject in managementObject)
{
var management1 = (ManagementObject)managementBaseObject;
if (!(bool)management1["IPEnabled"]) continue;
Array array = (Array)(management1.Properties["IpAddress"].Value);
IPv4Str = array.GetValue(0).ToString();
break;
}
return IPv4Str;
}
using System.Text.RegularExpressions;
using System.Diagnostics;
/// <summary>
/// 获取IP v4地址
/// </summary>
/// <returns>IPv4地址</returns>
/// 使用正则表达式在info中获取ipv4 地址,可以修改正则表达式来获取ipv6地址
private static string GetIPv4()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "ipconfig.exe";//设置程序名
cmd.StartInfo.Arguments = "/all"; //参数
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;//不显示窗口(控制台程序是黑屏)
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.Close();
Match m = Regex.Match(info, @"\d+\.\d+\.\d+\.\d+");
var ipString = m.ToString();
return ipString;
}
using System;
using System.Text.RegularExpressions;
using System.Diagnostics;
/// <summary>
/// 获取IPV6地址
/// </summary>
/// <returns></returns>
public static string GetIPV6()
{
Process cmd = new Process();
cmd.StartInfo.FileName = "ipconfig.exe";//设置程序名
cmd.StartInfo.Arguments = "/all"; //参数
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;//不显示窗口(控制台程序是黑屏)
cmd.Start();
string info = cmd.StandardOutput.ReadToEnd();
string[] de = new string[info.Length];
cmd.WaitForExit();
cmd.Close();
string patten = (
@"((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:)
{6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:
((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:
((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:
((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:
((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:
((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]
\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?");
Match m = Regex.Match(info, patten);
var ipString = m.ToString();
var v = ipString.Split('(');//获取到的IPV6地址后面会有一个(首选),在这里把他去掉
return v[0];
}
/// <summary>
/// 获取计算机名
/// </summary>
/// <returns>计算机名称</returns>
public static string GetComputerName()
{
return Environment.MachineName;
}
/// <summary>
/// 操作系统类型
/// </summary>
/// <returns>操作系统类型</returns>
public static string GetSystemType()
{
var sysTypeStr = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementBaseObject o in moc)
{
ManagementObject mo = (ManagementObject)o;
sysTypeStr = mo["SystemType"].ToString();
}
return sysTypeStr;
}
/// <summary>
/// 物理内存的容量
/// </summary>
/// <returns>物理内存,返回值类型为float,16GB内存返回15.875GB</returns>
public static float GetPhysicalMemory()
{
float memoryCount = 0;
var mc = new ManagementClass("Win32_ComputerSystem");
var moc = mc.GetInstances();
foreach (var o in moc)
{
var mo = (ManagementObject)o;
string str = mo["TotalPhysicalMemory"].ToString();//单位为 B
float a = long.Parse(str);
memoryCount = a / 1024 / 1024 / 1024;//单位换成GB
}
return memoryCount;
}
/// <summary>
/// 内存的数量,
/// </summary>
/// <returns>内存条的根数</returns>
public static int MemoryNumberCount()
{
ManagementClass m = new ManagementClass("Win32_PhysicalMemory");//内存条
ManagementObjectCollection mn = m.GetInstances();
int count = mn.Count;
return count;
}
/// <summary>
/// 主板制造厂商
/// </summary>
/// <returns>主板制造商</returns>
public static string GetBoardManufacturer()
{
SelectQuery query = new SelectQuery("Select * from Win32_BaseBoard");
ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
ManagementObjectCollection.ManagementObjectEnumerator data = mos.Get().GetEnumerator();
data.MoveNext();
ManagementBaseObject board = data.Current;
string baseBoradMaker = board.GetPropertyValue("Manufacturer").ToString();
return baseBoradMaker;
}
/// <summary>
/// 主板编号
/// </summary>
/// <returns></returns>
public static string GetBoardId()
{
string str = "";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
foreach (var o in mos.Get())
{
ManagementObject mo = (ManagementObject)o;
str = mo["SerialNumber"].ToString();
}
return str;
}
/// <summary>
/// 主板型号
/// </summary>
/// <returns></returns>
public static string GetBoardType()
{
string str ="";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
foreach (var o in mos.Get())
{
ManagementObject mo = (ManagementObject)o;
str = mo["Product"].ToString();
}
return str;
}
/// <summary>
/// CPU序列号
/// </summary>
/// <returns></returns>
private static string GetCPUNO()
{
string CPU_NO = "";//cpu序列号
ManagementClass managementClass = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = managementClass.GetInstances();
foreach (ManagementObject mo in moc)
{
CPU_NO = mo.Properties["ProcessorId"].Value.ToString();
}
return CPU_NO;
}
/// <summary>
/// 获取GPU名称,型号
/// </summary>
private static string GPUName()
{
string DisplayName = "";
ManagementClass m = new ManagementClass("Win32_VideoController");
ManagementObjectCollection mn = m.GetInstances();
DisplayName = "显卡数量:" + mn.Count.ToString() + " " + "\n";
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_VideoController");//Win32_VideoController 显卡
int count = 0;
foreach (ManagementObject mo in mos.Get())
{
count++;
DisplayName += "第" + count.ToString() + "张显卡名称:" + mo["Name"].ToString() + " " + "\n";
}
mn.Dispose();
m.Dispose();
return DisplayName;
}
/// <summary>
/// 获取主硬盘的名称
/// </summary>
/// <returns></returns>
private static string GETDiskName()
{
try
{
string DiskID = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
DiskID = mo.Properties["Model"].Value.ToString();
}
mc = null;
moc = null;
return DiskID;
}
catch
{
return "Failed to get disk name";
}
finally
{
}
}
/// <summary>
/// 硬盘的序列号
/// </summary>
/// <returns></returns>
public static string GetHardDiskSerialNumber()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM win32_DiskDrive");
string sHardDiskSerialNumber = "";
foreach (ManagementObject mo in searcher.Get())
{
sHardDiskSerialNumber = sHardDiskSerialNumber + mo["SerialNumber"].ToString().Trim() + "\n";
}
return sHardDiskSerialNumber;
}
catch
{
return "The hard disk serial number was not obtained.";
}
}
/// <summary>
/// 获取每一个硬盘的容量,单位GB
/// </summary>
/// <returns></returns>
public static string GetDiskCapacity()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM win32_DiskDrive");
string HardDriveCapacity = "";
foreach (ManagementObject mo in searcher.Get())
{
long size_B = long.Parse(mo["Size"].ToString());//
float sizeGB = size_B / 1000 / 1000 / 1000;
HardDriveCapacity = sizeGB.ToString() + "\n" + HardDriveCapacity;
}
return HardDriveCapacity;
}
catch
{
return "Failed to get the capacity of each disk.";
}
}
/// <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>
/// 获取BIOS信息,
/// </summary>
/// <returns></returns>
public static string GetBIOS()
{
string BIOSStr ="";
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("Select SerialNumber From Win32_BIOS");
ManagementObjectCollection moc = searcher.Get();
if (moc.Count > 0)
{
foreach (ManagementObject share in moc)
{
BIOSStr = share["SerialNumber"].ToString();
}
}
return BIOSStr;
}
以上参数方法 可以直接调用
免责声明
本学习笔记旨在提供编程语言和相关主题的信息和资源,帮助读者学习和掌握编程技能。然而,请注意以下事项:
准确性和完整性:尽管本博客内容经过精心编写和研究,但作者无法保证信息始终准确、完整或最新。编程语言和技术领域不断发展,读者应自行核实和查阅官方文档以确保信息的最新和准确。
文章评论