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. idea-git同步服务器代码后撤销操作

    工具:IntelliJ IDEA 2019.2.1 x64 记录一次不小心同步代码后,如何撤销操作. 1.同步服务器代码,右击项目,点击Synchronize 'xxx',如下: 2.打开Versio ...

  2. 2019/12/5BJFirstDay--scrum后台+cpp项目前台环境跑起来!!!

    1.配置服务器: 2.进入cd C:\java\25.beijing\06.vuejs\cpp201911221829\cpp 3.运行的命令是:npm run dev 4.先启动 5.然后再启动cp ...

  3. webuploader+web如何实现分片+断点续传

    众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路. 实现文件夹 ...

  4. Ubuntu安装php7.0环境

    1.下载必须组件 sudo apt-get install libxml2-dev sudo apt-get install curl 参考文献:http://php.net/manual/zh/in ...

  5. 共享库的使用(.so)文件

    1.共享库的概念 2.创建共享库命令 gcc -shared -fpci -o lib/libmath.so obj/mymath.o 具体加不加 fpci 这个要看平台支持吧支持:具体详情可以查阅 ...

  6. python写一个随机点名软件

    最近有个随机点名软件的需求,故写了一个,上代码:github地址 # -*- coding: utf-8 -*- # @Time : 18-12-31 下午4:21 # @Author : Felix ...

  7. C 库函数 - strchr()

    定义 char *strchr(const char *str, int c) 参数 str -- 要被检索的 C 字符串. c -- 在 str 中要搜索的字符 说明 该函数返回在字符串 str 中 ...

  8. phpstorm 2019.1 修改浏览器

    如图,修改如下浏览器的位置,由于我安装了虚拟机,导致每次点击谷歌浏览器后,都是打开的虚拟机里面的谷歌浏览器,需要重新设置浏览器的位置 打开设置 打开浏览器设置界面 双击可以选择浏览器的路径,然后就可以 ...

  9. Java 基础:抽象类与接口

    1.什么是抽象 当父类的某些方法不确定时,可以用abstract关键字来修饰该方法[抽象方法],用abstract来修饰该类[抽象类]. 我们都知道,父类是将子类所共同拥有的属性和方法进行抽取,这些属 ...

  10. radio得值

    $('input[name="ylqxjylcldnbModel.jylb"]:checked').val();   <input type="radio" ...