using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net;
|
using System.Net.NetworkInformation;
|
using System.Net.Sockets;
|
|
public class SystemInfoHelper
|
{
|
/// <summary>
|
/// 获取计算机用户名
|
/// </summary>
|
public static string GetComputerUserName()
|
{
|
return Environment.UserName;
|
}
|
|
/// <summary>
|
/// 获取计算机名
|
/// </summary>
|
public static string GetComputerName()
|
{
|
return Environment.MachineName;
|
}
|
|
/// <summary>
|
/// 获取所有MAC地址
|
/// </summary>
|
public static List<string> GetAllMacAddresses()
|
{
|
List<string> macAddresses = new List<string>();
|
|
try
|
{
|
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
|
foreach (NetworkInterface ni in interfaces)
|
{
|
if (ni.OperationalStatus == OperationalStatus.Up &&
|
ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
{
|
PhysicalAddress address = ni.GetPhysicalAddress();
|
if (address != null && address.ToString().Length > 0)
|
{
|
string mac = FormatMacAddress(address.ToString());
|
macAddresses.Add(mac);
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine("获取MAC地址错误: " + ex.Message);
|
}
|
|
return macAddresses;
|
}
|
|
/// <summary>
|
/// 获取第一个可用的MAC地址
|
/// </summary>
|
public static string GetFirstMacAddress()
|
{
|
var macAddresses = GetAllMacAddresses();
|
return macAddresses.Count > 0 ? macAddresses[0] : string.Empty;
|
}
|
|
/// <summary>
|
/// 获取所有IP地址(IPv4)
|
/// </summary>
|
public static List<string> GetAllIPAddresses()
|
{
|
List<string> ipAddresses = new List<string>();
|
|
try
|
{
|
string hostName = Dns.GetHostName();
|
IPAddress[] addresses = Dns.GetHostAddresses(hostName);
|
|
foreach (IPAddress address in addresses)
|
{
|
if (address.AddressFamily == AddressFamily.InterNetwork) // IPv4
|
{
|
ipAddresses.Add(address.ToString());
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine("获取IP地址错误: " + ex.Message);
|
}
|
|
return ipAddresses;
|
}
|
|
/// <summary>
|
/// 获取本地IP地址(排除127.0.0.1)
|
/// </summary>
|
public static string GetLocalIPAddress()
|
{
|
var ipAddresses = GetAllIPAddresses();
|
foreach (string ip in ipAddresses)
|
{
|
if (ip != "127.0.0.1"
|
&& !ip.StartsWith("169.254.")
|
&& !ip.StartsWith("192.168.")) // 排除回环和APIPA地址
|
{
|
return ip;
|
}
|
}
|
return ipAddresses.Count > 0 ? ipAddresses[0] : string.Empty;
|
}
|
|
/// <summary>
|
/// 格式化MAC地址(添加-分隔符)
|
/// </summary>
|
private static string FormatMacAddress(string mac)
|
{
|
if (string.IsNullOrEmpty(mac) || mac.Length != 12)
|
return mac;
|
|
// 将 "001122AABBCC" 格式化为 "00-11-22-AA-BB-CC"
|
return string.Format("{0}-{1}-{2}-{3}-{4}-{5}",
|
mac.Substring(0, 2),
|
mac.Substring(2, 2),
|
mac.Substring(4, 2),
|
mac.Substring(6, 2),
|
mac.Substring(8, 2),
|
mac.Substring(10, 2));
|
}
|
/// <summary>
|
/// 获取所有系统信息
|
/// </summary>
|
public static Dictionary<string, object> GetAllSystemInfo()
|
{
|
return new Dictionary<string, object>
|
{
|
{ "UserName", GetComputerUserName() },
|
{ "ComputerName", GetComputerName() },
|
{ "MACAddresses", GetAllMacAddresses() },
|
{ "FirstMACAddress", GetFirstMacAddress() },
|
{ "IPAddresses", GetAllIPAddresses() },
|
{ "LocalIPAddress", GetLocalIPAddress() },
|
{ "OSVersion", Environment.OSVersion.ToString() },
|
{ "SystemDirectory", Environment.SystemDirectory },
|
{ "ProcessorCount", Environment.ProcessorCount },
|
{ "CurrentTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }
|
};
|
}
|
}
|