C# 获取本机mac地址 客户端主机名称(hostName) 当前用户(CurWinUser) 操作系统版本(

时间:2022-05-25 00:13:52

关于获取本机信息的代码,园子里面还是非常多的,专门整理了一下此次用到的信息

首先,获取跳至网管的IP地址

#region 获取调至网管的IP地址 string ipAddress = GetLocalIp(); #endregion ///此方法需要计算机连网,否则获取不到IP地址 private string GetLocalIp() { string result = RunApp("route", "print", true); Match m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)"); if (m.Success) { return m.Groups[2].Value; } else { try { System.Net.Sockets.TcpClient c = new System.Net.Sockets.TcpClient(); c.Connect("www.baidu.com", 80); string ip = ((System.Net.IPEndPoint)c.Client.LocalEndPoint).Address.ToString(); c.Close(); return ip; } catch (Exception) { return null; } } } public static string RunApp(string filename, string arguments, bool recordLog) { try { if (recordLog) { Trace.WriteLine(filename + " " + arguments); } Process proc = new Process(); proc.StartInfo.FileName = filename; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.Arguments = arguments; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.UseShellExecute = false; proc.Start(); using (System.IO.StreamReader sr = new System.IO.StreamReader(proc.StandardOutput.BaseStream, Encoding.Default)) { string txt = sr.ReadToEnd(); sr.Close(); if (recordLog) { Trace.WriteLine(txt); } if (!proc.HasExited) { proc.Kill(); } return txt; } } catch (Exception ex) { Trace.WriteLine(ex); return ex.Message; } }

获取本机mac地址 (获取连接网络网卡的mac地址)

园子这篇文章写的很好,,,我也是参考这个的

string macAddre = GetMacBySendARP(ipAdd); ///<summary> /// 通过SendARP获取网卡Mac /// 网络被禁用或未接入网络(如没插网线)时此方法失灵 ///</summary> ///<param></param> ///<returns></returns> public static string GetMacBySendARP(string remoteIP) { StringBuilder macAddress = new StringBuilder(); try { Int32 remote = inet_addr(remoteIP); Int64 macInfo = new Int64(); Int32 length = 6; SendARP(remote, 0, ref macInfo, ref length); string temp = Convert.ToString(macInfo, 16).PadLeft(12, ‘0‘).ToUpper(); int x = 12; for (int i = 0; i < 6; i++) { if (i == 5) { macAddress.Append(temp.Substring(x - 2, 2)); } else { macAddress.Append(temp.Substring(x - 2, 2) + "-"); } x -= 2; } return macAddress.ToString(); } catch { return macAddress.ToString(); } } [DllImport("Iphlpapi.dll")] privatestaticexternint SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length); [DllImport("Ws2_32.dll")] privatestaticextern Int32 inet_addr(string ip);

客户端主机名称和当前电脑用户

#region 获取客户端主机名称 string hostName = Dns.GetHostName(); #endregion #region 获取电脑当前用户 string curWinUser=System.Environment.UserName.ToString(); #endregion

操作系统版本(WinVersion)  IE浏览器版本(IEversion)

#region 操作系统版本 string winVersion = GetCurSysVersion(); #endregion //获取当前操作系统版本 private string GetCurSysVersion() { string userAgent = Environment.OSVersion.ToString(); string osVersion = "未知"; if (userAgent.Contains("NT 10.0")) { osVersion = "Windows 10"; } else if (userAgent.Contains("NT 6.3")) { osVersion = "Windows 8.1"; } else if (userAgent.Contains("NT 6.2")) { osVersion = "Windows 8"; } else if (userAgent.Contains("NT 6.1")) { osVersion = "Windows 7"; } else if (userAgent.Contains("NT 6.1")) { osVersion = "Windows 7"; } else if (userAgent.Contains("NT 6.0")) { osVersion = "Windows Vista/Server 2008"; } else if (userAgent.Contains("NT 5.2")) { if (userAgent.Contains("64")) osVersion = "Windows XP"; else osVersion = "Windows Server 2003"; } else if (userAgent.Contains("NT 5.1")) { osVersion = "Windows XP"; } else if (userAgent.Contains("NT 5")) { osVersion = "Windows 2000"; } else if (userAgent.Contains("NT 4")) { osVersion = "Windows NT4"; } else if (userAgent.Contains("Me")) { osVersion = "Windows Me"; } else if (userAgent.Contains("98")) { osVersion = "Windows 98"; } else if (userAgent.Contains("95")) { osVersion = "Windows 95"; } else if (userAgent.Contains("Mac")) { osVersion = "Mac"; } else if (userAgent.Contains("Unix")) { osVersion = "UNIX"; } else if (userAgent.Contains("Linux")) { osVersion = "Linux"; } else if (userAgent.Contains("SunOS")) { osVersion = "SunOS"; } else { osVersion = userAgent; } return osVersion; } #region IE浏览器版本 string IEversion = GetCurIEVersion(); #endregion /// <summary> /// 获取当前IE版本 /// </summary> /// <returns></returns> private string GetCurIEVersion() { RegistryKey mreg; mreg = Registry.LocalMachine; mreg = mreg.CreateSubKey("software\\Microsoft\\Internet Explorer"); string IEVersion = (String)mreg.GetValue("Version"); mreg.Close(); return IEVersion; }

物理内存使用情况