posts - 710,  comments - 81,  views - 260万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

获取计算机信息,获取远程计算机信息的时候需要关闭远程机器的防火墙,否则获取不到相关信息。

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using System.Runtime.InteropServices;

/// <summary>
///NetHelper 的摘要说明
/// </summary>
public class NetHelper
{
    public NetHelper()
    {

    }

    public static string GetBrowserType()
    {
        return HttpContext.Current.Request.Browser.Type;
    }

    public static string GetSysVersion()
    {
        string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

        if (Agent.IndexOf("NT 4.0") > 0)
        {
            return "Windows NT ";
        }
        else if (Agent.IndexOf("NT 5.0") > 0)
        {
            return "Windows 2000";
        }
        else if (Agent.IndexOf("NT 5.1") > 0)
        {
            return "Windows XP";
        }
        else if (Agent.IndexOf("NT 5.2") > 0)
        {
            return "Windows 2003";
        }
        else if (Agent.IndexOf("NT 6.0") > 0)
        {
            return "Windows Vista";
        }
        else if (Agent.IndexOf("WindowsCE") > 0)
        {
            return "Windows CE";
        }
        else if (Agent.IndexOf("NT") > 0)
        {
            return "Windows NT ";
        }
        else if (Agent.IndexOf("9x") > 0)
        {
            return "Windows ME";
        }
        else if (Agent.IndexOf("98") > 0)
        {
            return "Windows 98";
        }
        else if (Agent.IndexOf("95") > 0)
        {
            return "Windows 95";
        }
        else if (Agent.IndexOf("Win32") > 0)
        {
            return "Win32";
        }
        else if (Agent.IndexOf("Linux") > 0)
        {
            return "Linux";
        }
        else if (Agent.IndexOf("SunOS") > 0)
        {
            return "SunOS";
        }
        else if (Agent.IndexOf("Mac") > 0)
        {
            return "Mac";
        }
        else if (Agent.IndexOf("Linux") > 0)
        {
            return "Linux";
        }
        else if (Agent.IndexOf("Windows") > 0)
        {
            return "Windows";
        }
        return "unknow";

    }


    /// <summary>
    /// 如果有代理那么越过代理直接取值
    /// </summary>
    /// <returns></returns>
    public static string GetClientIp()
    {
        if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        else
            return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
    }

    public static string GetHostName()
    {
        return GetHostName(GetClientIp());
    }

    public static string GetMac()
    {
        return GetMac(GetClientIp());
    }


    public static string GetHostName(string ipStr)
    {
        
        string hostName = string.Empty;
        try
        {
            System.Net.IPAddress ip = System.Net.IPAddress.Parse(ipStr);
            System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(ip);
            hostName = host.HostName;
        }
        catch
        { }
        return hostName;
    }



    [DllImport("Iphlpapi.dll")]
    static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen);
    [DllImport("Ws2_32.dll")]
    static extern Int32 inet_addr(string ipaddr);
    /// <summary> 
    /// SendArp获取MAC地址 
    /// </summary> 
    /// <param name="RemoteIP">目标机器的IP地址如(192.168.1.1)</param> 
    /// <returns>目标机器的mac 地址</returns> 
    public static string GetMac(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();
        }
    }
}
复制代码

 程序员的基础教程:菜鸟程序员

posted on   itprobie-菜鸟程序员  阅读(738)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示