c# 擷取電腦資訊

來源:互聯網
上載者:User

標籤:

擷取電腦資訊,擷取遠端電腦資訊的時候需要關閉遠程機器的防火牆,否則擷取不到相關資訊。

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();        }    }}

 

c# 擷取電腦資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.