using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets; namespace Common
{
public class NetworkHelper
{
/// <summary></summary>
/// 显示本机各网卡的详细信息
/// <summary></summary>
public static void ShowNetworkInterfaceMessage()
{
NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in fNetworkInterfaces)
{
#region " 网卡类型 "
string fCardType = "未知网卡";
string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
// 区分 PnpInstanceID
// 如果前面有 PCI 就是本机的真实网卡
// MediaSubType 为 01 则是常见网卡,02为无线网卡。
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 &&
fPnpInstanceID.Substring(0, 3) == "PCI")
fCardType = "物理网卡";
else if (fMediaSubType == 1)
fCardType = "虚拟网卡";
else if (fMediaSubType == 2)
fCardType = "无线网卡";
}
#endregion
#region 网卡信息
System.Console.WriteLine("-----------------------------------------------------------");
System.Console.WriteLine("-- " + fCardType);
System.Console.WriteLine("-----------------------------------------------------------");
System.Console.WriteLine("Id .................. : {0}", adapter.Id); // 获取网络适配器的标识符
System.Console.WriteLine("Name ................ : {0}", adapter.Name); // 获取网络适配器的名称
System.Console.WriteLine("Description ......... : {0}", adapter.Description); // 获取接口的描述
System.Console.WriteLine("Interface type ...... : {0}", adapter.NetworkInterfaceType); // 获取接口类型
System.Console.WriteLine("Is receive only...... : {0}", adapter.IsReceiveOnly); // 获取 Boolean 值,该值指示网络接口是否设置为仅接收数据包。
System.Console.WriteLine("Multicast............ : {0}", adapter.SupportsMulticast); // 获取 Boolean 值,该值指示是否启用网络接口以接收多路广播数据包。
System.Console.WriteLine("Speed ............... : {0}", adapter.Speed); // 网络接口的速度
System.Console.WriteLine("Physical Address .... : {0}", adapter.GetPhysicalAddress().ToString()); // MAC 地址 IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
{
if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
System.Console.WriteLine("Ip Address .......... : {0}", UnicastIPAddressInformation.Address); // Ip 地址
}
System.Console.WriteLine();
#endregion
}
System.Console.ReadKey();
} /// <summary>
/// 获得本机真实物理网卡IP
/// </summary>
/// <returns></returns>
public static IList<string> GetPhysicsNetworkCardIP()
{
var networkCardIPs = new List<string>(); NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in fNetworkInterfaces)
{
string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
// 区分 PnpInstanceID
// 如果前面有 PCI 就是本机的真实网卡
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
{
IPInterfaceProperties fIPInterfaceProperties = adapter.GetIPProperties();
UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = fIPInterfaceProperties.UnicastAddresses;
foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
{
if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
{
networkCardIPs.Add(UnicastIPAddressInformation.Address.ToString()); //Ip 地址
}
}
}
}
} return networkCardIPs;
}
}
}

  

C#读取物理网卡信息及其对应的IP地址的更多相关文章

  1. 使用powershell为物理网卡添加多个IP地址

    因特殊要求,需要给某物理网卡添加多个IP地址: powershell中有个netsh的命令,添加IPv4地址的方法: add address [name=]<字符串>       [[ad ...

  2. python 跨平台获取网卡信息和本机ip地址

    笔者在项目中遇到过获取本机网卡ip的例子,利用python库psutil解决了此问题. def get_netcard(): """获取网卡名称和ip地址 "& ...

  3. Linux/CentOS服务器 一个网卡绑定多IP地址(永久设置)

    有时我们在使用 Linux 服务器时需要配置多个IP地址.如果要配置多个IP地址是否需要多块网卡呢?答案是否定的.以 CentOS 系统为例,多个 IP 地址是可以共享一块物理网卡的. 如何永久为单网 ...

  4. linux ip别名和辅助ip地址

    转:https://blog.csdn.net/xiewen99/article/details/54729112?utm_source=itdadao&utm_medium=referral ...

  5. 读取本地外网IP地址

    读取本地外网IP地址. 根据启动并运行的网卡名称,找到本机实际的IP地址(已知当前运行的无线网卡名包含某一个字符) import java.net.InterfaceAddress; import j ...

  6. php读取qqwry.dat ip地址定位文件的类

    <?php// +----------------------------------------------------------------------// |// +---------- ...

  7. python绝技 — 使用PyGeoIP关联IP地址和物理位置

    准备工作 要关联IP与物理位置,我们需要有一个包含这样对应关系的数据库. 我们可以使用开源数据库GeoLiteCity,它能够较为准确地把IP地址与所在城市关联起来 下载地址:http://dev.m ...

  8. 转:为什么根据IP地址查询物理所在地,而不是mac地址?

    来自 https://mp.weixin.qq.com/s/aOZQGMnMI2nkX4-qcJL4WQ 读者 不是说mac地址是计算机网卡唯一的地址吗?这样不是可以直接定位到某一台机器吗?为什么要用 ...

  9. java通过IP地址获取物理位置

    import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern ...

随机推荐

  1. Kali Linux Web 渗透测试— 第十二课-websploit

    Kali Linux Web 渗透测试— 第十二课-websploit 文/玄魂 目录 Kali Linux Web 渗透测试— 第十二课-websploit..................... ...

  2. [JS3] 立即执行JS

    <html> <head> <title>立即执行</title> <SCRIPT TYPE="text/JavaScript" ...

  3. Linux:常用shell快捷键

    按键 作用 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂定当前程序,暂停后按下任意键恢复运行 Ctrl+z 将当前程序放到后台运行,恢复到前台为命令fg Ctrl+a 将光标移至输入行头,相 ...

  4. my linux tech object

    I want to be a linux kernel development engineer. That's my dream.

  5. Essential C++中文版 前言

    天啊,这本书竟是如此轻薄短小.我真想大叫一声“哇欧”!C++ Primer 加上索引.扉页.谢词之后,厚达1237 页,而此书却只有薄薄276 页.套句拳击术语,这是一部“轻量级”作品. 每个人都会好 ...

  6. Javascript实现计数器,定时警告和停止

    <html> <head> <meta charset="utf-8"> <title>定时警告</title> < ...

  7. Explain in detail the steps/processes that occur from the moment you type a URL in a browser and hit enter

    In an extremely rough and simplified sketch, assuming the simplest possible HTTP request, no proxies ...

  8. raid性能对比

    1,raid0的特性:采用剥离,数据将在几个磁盘上进行分割.数据被分成很多数据块,每一数据块会被写入不同的磁盘.从而, 每一磁盘的工作负荷都得到了降低,这有助于加速数据传输.RAID-0可让磁盘更好地 ...

  9. Java 7 jstat – JVM Statistics Monitoring Tool【翻译】

    原文地址:Java 7 jstat 本文内容 语法 参数 描述 虚拟机标识符 选项 一般选项 输出选项 示例 先发出来,然后慢慢翻译~ 语法 jstat [ generalOption | outpu ...

  10. HTTP 错误 500.21 - Internal Server Error 处理程序“ExtensionlessUrlHandler-ISAPI-4.0_64bit”在其模块列表中有一个错误模块“IsapiModule” 解决方法

    IIS在发布网站后找不到首页,提示以上错误,原因是在“应用程序池”中,把对应的网站的“托管管道模式”设置为“集成”即可.