C#获取IP及MAC地址 方法,比较齐全

using System.Net;
using System;
using System.Management;
using System.Runtime.InteropServices; public class getIP
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip); //获取本机的IP public string getLocalIP()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP string strAddr = ipEntry.AddressList[].ToString();
return (strAddr);
}
//获取本机的MAC public string getLocalMac()
{
string mac = null;
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
return (mac);
} //获取远程主机IP public string[] getRemoteIP(string RemoteHostName)
{
IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);
IPAddress[] IpAddr = ipEntry.AddressList;
string[] strAddr = new string[IpAddr.Length];
for (int i = ; i < IPADDR.LENGTH; I++)
{
strAddr[i] = IpAddr[i].ToString();
}
return (strAddr);
}
//获取远程主机MAC public string getRemoteMac(string localIP, string remoteIP)
{
Int32 ldest = inet_addr(remoteIP); //目的ip Int32 lhost = inet_addr(localIP); //本地ip try
{
Int64 macinfo = new Int64();
Int32 len = ;
int res = SendARP(ldest, , ref macinfo, ref len);
return Convert.ToString(macinfo, );
}
catch (Exception err)
{
Console.WriteLine("Error:{0}", err.Message);
}
return .ToString();
} public static void Main(string[] args)
{
getIP gi = new getIP();
Console.WriteLine("本地网卡信息:");
Console.WriteLine(gi.getLocalIP() + " - " + gi.getLocalMac()); Console.WriteLine("\n\r远程网卡信息:");
string[] temp = gi.getRemoteIP("scmobile-tj2");
for (int i = ; i < TEMP.LENGTH; I++)
{
Console.WriteLine(temp[i]);
}
Console.WriteLine(gi.getRemoteMac("192.168.0.3", "192.168.0.1"));
}
}

C#获取IP及MAC地址 方法的更多相关文章

  1. C语言实现Windows下获取IP和MAC地址。

    C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...

  2. Android -- 获取IP和MAC地址

    通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法: 首先新建一个工程,修改AndroidManifest.xml文件 ...

  3. 获取IP和mac地址

    1.获取IP static string GetLocalIp() { string hostname = Dns.GetHostName();//得到本机名 //IPHostEntry localh ...

  4. .net获取IP和MAC地址

    获取IP  解决request.UserHostAddress取不到真实IP private string GetClientIP()   {    string result = HttpConte ...

  5. js获取IP和MAC地址

    1.IP 百度一下有很多 可以用这个 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> ...

  6. 使用ARP获取局域网内设备IP和MAC地址

    根据Arp列表数据,查询本地设备在线状态 使用 arp -a 获得所有内网地址,首先看Mod对象 public struct MacIpPair { public string HostName; p ...

  7. 获取客户机的ip和mac地址

    只获取clientIP package com.ppms.utils; import javax.servlet.http.HttpServletRequest; /** * Created by l ...

  8. 获取本机IP、mac地址、计算机名

    python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...

  9. Linux 获取本机IP、MAC地址用法大全

    getifaddrs()和struct ifaddrs的使用,获取本机IP ifaddrs结构体定义如下: struct ifaddrs { struct ifaddrs *ifa_next; /* ...

随机推荐

  1. thinkpadT470P安装问题

    [问题描述]: 最近在将Thinkpad E430c的ubuntu系统重装成windows 7的过程中,出现了装好win7系统后,开机自动进入boot menu界面的问题,而且不论你选择从光驱还是硬盘 ...

  2. php自定义函数之静态变量

    如果我想知道函数被调用了多少次怎么办?在没有学习静态变量的时候,我们没有好的办法来解决. 静态变量的特点是:声明一个静态变量,第二次调用函数的时候,静态变量不会再初始化变量,会在原值的基础上读取执行. ...

  3. Error Permission denied when running brew cleanup

    Error Permission denied when running brew cleanup When I try to run `brew cleanup` I get: Warning: S ...

  4. Navicat连接mysql报错1251 -client does not support authentication protocol

    原文https://blog.csdn.net/qq_35654080/article/details/82588188 详解请参考https://blog.csdn.net/chszs/articl ...

  5. SDOI2015做题记录

    由于我懒,并且这里面除了D2T3恶心以外都不难写,所以很多代码都没写-- 排序 对于某一个合法的操作序列(操作序列定义为每次交换的两组数),可以随意交换顺序,仍然合法.所以对于一个操作集合,答案就加\ ...

  6. 备用shell管理方案之butterfly+nginx+https

    安装butterfly+nginx https 1. 安装butterfly yum install python36 python36-pip python36-devel -y pip insta ...

  7. pipres生成当前项目所有的依赖文件

    对于使用虚拟环境的Python程序,直接pip freeze即可.但是对于没有使用虚拟环境,再使用pip freeze就不行了,因为它会把系统所有的包都导出. 所以使用第三方库pipreqs 安装 p ...

  8. mongo最大连接数查看

    进入客户端 mongo 输入查看命令 db.serverStatus().connections

  9. C# WinForm中的一些小问题

    前言 记一些C#窗体应用编程中的小问题. DataGridView 设置Selected = true后实际选中行标依旧是第一行. 通常通过this.dataGridView1.Rows[i].Sel ...

  10. H5注意点(2)

    1. 列表标签的作用:给一堆数据添加列表语义,也就是告诉搜索引擎告诉浏览器这一堆数据是一个整体. 2. HTML中列表标签的分类 - 无序列表(最多)(unordered list)(格式:ul> ...