123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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 {
- /// <summary>
- /// 获取本机MAC地址
- /// </summary>
- /// <returns>本机MAC地址</returns>
- 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";
- }
- }
- /// <summary>
- /// 操作系统的登录用户名
- /// </summary>
- /// <returns>系统的登录用户名</returns>
- 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";
- }
- }
- /// <summary>
- /// 获取客户端内网IPv4地址
- /// </summary>
- /// <returns>客户端内网IPv4地址</returns>
- 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";
- }
- }
- }
- }
|