zoukankan      html  css  js  c++  java
  • C# 获取本地IP客户端IP地址以及MAC地址

    --------------获得客户端IP

       /// <summary>
            /// 获取客户端IP
            /// </summary>
            /// <returns></returns>
            public static string GetClientIP()
            {
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                return result;
            }

    
    
    C# 获取本地IP地址以及MAC地址
    1、通过主机名获取局域网IP地址;
    try
    {
    IPHostEntry ipHostEntry
    = Dns.GetHostEntry(txtHost.Text);//传递计算机名
    if (ipHostEntry.Aliases.Length >0)
    {
    foreach (string alias in ipHostEntry.Aliases)
    txtIP.Text
    = alias;
    }
    //获取IP地址
    foreach (IPAddress addr in ipHostEntry.AddressList)
    txtIPAddress.Text
    = addr.ToString();//获取IP地址
    }
    catch
    {
    MessageBox.Show(
    "错误的主机名。");
    }
    2、通过局域网IP地址获取主机名;
    try
    {
    IPHostEntry ipHostEntry
    = Dns.GetHostByAddress(txtIP.Text); ;//传递IP地址
    txtHostName.Text = ipHostEntry.HostName.ToString()//取得主机名
    }
    catch
    {
    MessageBox.Show(
    "错误的IP地址。");
    }
    3、通过局域网IP地址获取Mac地址
    publicstaticstring ReadMac(string ip)//传递IP地址,即可返回MAC地址
    {
    string mac ="";
    System.Diagnostics.Process p
    =new System.Diagnostics.Process();
    p.StartInfo.FileName
    ="nbtstat";
    p.StartInfo.Arguments
    ="-a "+ ip;
    p.StartInfo.UseShellExecute
    =false;
    p.StartInfo.CreateNoWindow
    =true;
    p.StartInfo.RedirectStandardOutput
    =true;
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    int len = output.IndexOf("MAC Address = ");
    if(len>0)
    {
    mac
    = output.Substring(len +14, 17);
    }
    p.WaitForExit();
    return mac;
    }



    C#获取本地计算机名,IP,MAC地址

    using System;
    using System.Drawing;
    using System.Management;
    using System.net;
    using System.Net.Sockets;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace MAC_IP_name
    {
    ///<summary>
    /// Form1 的摘要说明。
    ///</summary>
    publicclass Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.GrouPBox groupBox1;
    private System.Windows.Forms.TextBox txtMac;
    private System.Windows.Forms.TextBox txtIp;
    private System.Windows.Forms.TextBox txtName;
    private System.Windows.Forms.Label lblMac;
    private System.Windows.Forms.Label lblIp;
    private System.Windows.Forms.Label lblName;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    ///<summary>
    /// 必需的设计器变量。
    ///</summary>
    private System.ComponentModel.Container components =null;

    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    }

    ///<summary>
    /// 清理所有正在使用的资源。
    ///</summary>
    protectedoverridevoid Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components !=null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    ///<summary>
    /// 应用程序的主入口点。
    ///</summary>
    [STAThread]
    staticvoid Main()
    {
    Application.Run(
    new Form1());
    }

    privatevoid button1_Click_1(object sender, System.EventArgs e)
    {
    string s="",mac="";
    //
    //name
    //
    string hostInfo = Dns.GetHostName();

    //
    //IP
    System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
    for (int i =0; i < addressList.Length; i ++)
    {
    s
    += addressList[i].ToString();
    }
    //
    //mac
    //


    ManagementClass mc;
    mc
    =new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc
    =mc.GetInstances();
    foreach(ManagementObject mo in moc)
    {
    if(mo["IPEnabled"].ToString()=="True")
    mac
    =mo["MacAddress"].ToString();
    }


    txtName.Text
    =hostInfo;
    txtIp.Text
    =s;
    txtMac.Text
    =mac;


    button1.Enabled
    =false;
    button2.Focus();

    }

    privatevoid button2_Click(object sender, System.EventArgs e)
    {
    Application.Exit();
    }
    }
    }
  • 相关阅读:
    JDK1.8十个新特性
    问题:No more handles [Could not detect registered XULRunner to use]
    在未标记为正在运行时,调用了RunScript
    问题: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    常用数据库的JDBC 的URL形式
    Myeclipse插件的三种安装方式
    mysqlError: Illegal connection port value '3306>]'
    异常:Bad version number in .class file
    安装msi文件遇到错误code=2502 or 2503 win8
    spring使用RedisTemplate的坑Could not get a resource from the pool
  • 原文地址:https://www.cnblogs.com/lfzm/p/2063055.html
Copyright © 2011-2022 走看看