using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Management; using System.Net; namespace Client { public class ClientInfo { public string MacAddr { get; set; } public string Ipv4 { get; set; } public string Name { get; set; } } public class ClientInfoUtil { /// /// 获取本机MAC地址 /// /// 本机MAC地址 public static string GetMacAddress() { try { string strMac = string.Empty; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { strMac = mo["MacAddress"].ToString(); } } moc = null; mc = null; return strMac; } catch { return "unknown"; } } /// /// 操作系统的登录用户名 /// /// 系统的登录用户名 public static string GetUserName() { try { return $"{Environment.MachineName}\\{Environment.UserName}"; //string strUserName = string.Empty; //ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); //ManagementObjectCollection moc = mc.GetInstances(); //foreach (ManagementObject mo in moc) //{ // strUserName = mo["UserName"].ToString(); //} //moc = null; //mc = null; //return strUserName; } catch { return "unknown"; } } /// /// 获取客户端内网IPv4地址 /// /// 客户端内网IPv4地址 public static string GetClientLocalIPv4Address() { string strLocalIP = string.Empty; try { IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHost.AddressList[0]; strLocalIP = ipAddress.ToString(); return strLocalIP; } catch { return "unknown"; } } } }