获取网络适配器的相关信息(包括网络连接名称)
来源:互联网 发布:恶魔的奶爸扒皮 知乎 编辑:程序博客网 时间:2024/06/02 21:07
目前获取网络连接信息的方法有很多种,我用的是wmi方法来获取,但却无法获得网络连接的名称(如本地连接),在网上找了找,终于找到一种不同的实现方式,特此与大家共享:
来源:CodeProject
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class IP_Adapter_Addresses
{
public uint Length;
public uint IfIndex;
public IntPtr Next;
public IntPtr AdapterName;
public IntPtr FirstUnicastAddress;
public IntPtr FirstAnycastAddress;
public IntPtr FirstMulticastAddress;
public IntPtr FirstDnsServerAddress;
public IntPtr DnsSuffix;
public IntPtr Description;
public IntPtr FriendlyName;
[MarshalAs(UnmanagedType.ByValArray,
SizeConst = 8)]
public Byte[] PhysicalAddress;
public uint PhysicalAddressLength;
public uint flags;
public uint Mtu;
public uint IfType;
public uint OperStatus;
public uint Ipv6IfIndex;
public uint ZoneIndices;
public IntPtr FirstPrefix;
}
[DllImport("Iphlpapi.dll")]
public static extern uint GetAdaptersAddresses(uint Family, uint flags, IntPtr Reserved,
IntPtr PAdaptersAddresses, ref uint pOutBufLen);
其中IP_Adapter_Addresses为网络适配器的相关信息,下面的代码会调用到:
string strAdapterInfo = string.Empty;
IntPtr PAdaptersAddresses = new IntPtr();
bool AdapterFound = false;
uint pOutLen = 100;
PAdaptersAddresses = Marshal.AllocHGlobal(100);
uint ret = GetAdaptersAddresses(0, 0, (IntPtr)0,
PAdaptersAddresses, ref pOutLen);
// if 111 error, use
if (ret == 111)
{
Marshal.FreeHGlobal(PAdaptersAddresses);
PAdaptersAddresses = Marshal.AllocHGlobal((int)pOutLen);
ret = GetAdaptersAddresses(0, 0, (IntPtr)0,
PAdaptersAddresses, ref pOutLen);
}
IP_Adapter_Addresses adds = new IP_Adapter_Addresses();
IntPtr pTemp = PAdaptersAddresses;
IntPtr pTempIP = new IntPtr();
while (pTemp != (IntPtr)0)
{
Marshal.PtrToStructure(pTemp, adds);
string adapterName = Marshal.PtrToStringAnsi(adds.AdapterName);
string FriendlyName = Marshal.PtrToStringAuto(adds.FriendlyName);
string tmpString = string.Empty;
for (int i = 0; i < 6; i++)
{
tmpString += string.Format("{0:X2}", adds.PhysicalAddress[i]);
if (i < 5)
{
tmpString += ":";
}
}
RegistryKey theLocalMachine = Registry.LocalMachine;
RegistryKey theSystem = theLocalMachine.OpenSubKey(@"SYSTEM/Current" +
@"ControlSet/Services/Tcpip/Parameters/Interfaces");
RegistryKey theInterfaceKey = theSystem.OpenSubKey(adapterName);
if (theInterfaceKey != null)
{
string DhcpIPAddress = (string)theInterfaceKey.GetValue("DhcpIPAddress");
// system is using DHCP
if (DhcpIPAddress != null)
{
string tArray = (string)
theInterfaceKey.GetValue("DhcpIPAddress", theInterfaceKey);
strAdapterInfo += "Network adapter: " +
FriendlyName.ToString() + "/r/n";
strAdapterInfo += "IP Address: " + tArray + "/r/n";
strAdapterInfo += "MAC Address: " + tmpString + "/r/n/r/n";
AdapterFound = true;
}
else
{
string[] tArray = (string[])
theInterfaceKey.GetValue("IPAddress", theInterfaceKey);
strAdapterInfo += "Network adapter: " +
FriendlyName.ToString() + "/r/n";
for (int Interface = 0; Interface < tArray.Length; Interface++)
{
strAdapterInfo += "IP Address: " +
tArray[Interface] + "/r/n";
AdapterFound = true;
}
strAdapterInfo += "MAC Address: " + tmpString + "/r/n/r/n";
}
}
pTemp = adds.Next;
}
if (AdapterFound != true)
{
strAdapterInfo += "No network adapters configured/present/r/n";
}
引用的命名空间包括:
using System;
using System.Collections;
using System.ComponentModel;
using Microsoft.Win32;
using System.Data;
using System.Runtime.InteropServices;
- 获取网络适配器的相关信息(包括网络连接名称)
- VB6获取本机网络适配器信息(网卡名称、IP地址、子网掩码、默认网关等)
- 网络适配器不见了网络连接不见了的解决方法
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第一节 获取本机网络适配器的信息
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第一节 获取本机网络适配器的信息
- ios获取当前连接的网络名称
- C# .NET 获取网络适配器信息
- 获取网络连接信息
- 【Window】获取电脑连接的网络信息
- 获取网络连接名称“本地连接”的两种方法
- 使用WMI控制网络适配器的类(获取所有启用的网络适配器及其信息。设置IP、子网掩码,网关,DNS)
- 利用IP_ADAPTER_INFO结构获取网络适配器信息
- Android 获取网络相关信息
- 获取Android网络连接信息
- 网络适配器相关
- Android获取网络的名称
- linux中获取MP3的相关信息的函数,包括总长时间等
- 安卓手机获取当前连接的WIFI相关信息
- jBPM用户指南 第十五章 记录日志 (转载)
- jBPM用户指南 第16章 jBPM流程定义语言 (转载)
- jBPM用户指南 第十八章 工作流TDD (转载)
- XML on Windows Mobile (C#)
- [Java][j2ee]log4j初试
- 获取网络适配器的相关信息(包括网络连接名称)
- Q版京剧脸谱来喽——花脸
- 用Apache JMeter进行性能测试之四(使用CSV元件)
- 在学习中工作
- Q版京剧脸谱来喽——刀马旦
- Shell学习笔记(二)
- Shell学习笔记(三)
- 自定义异常处理类
- 微软STL,hash_map 的效率问题