获取网络适配器的相关信息(包括网络连接名称)
2008-04-14 12:14
609 查看
目前获取网络连接信息的方法有很多种,我用的是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;
来源: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;
相关文章推荐
- 如何获取CPU的相关信息 包括CPU编号、版本、产品名称、制造商
- EntityFramework.SqlServer.dll 中发生 其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接)
- C# 获取适配器网络连接IP地址,子网掩码,DNS,数据包等信息
- 如何获取网卡相关信息(包括mac, ip, mask, gateway等)?---利用GetAdaptersInfo
- 【Java】【系统状态】获取系统信息(包括操作系统、jvm、cpu、内存、硬盘、网络、io等)
- android GSM+CDMA基站定位--获取移动网络相关信息
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服 务器。请验证实例名称是否正确并且 SQL Server 已配置为允
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许
- Python绝技笔记--------python操作注册表来获取之前连接的网络信息
- IOS获取自身网络信息的方法,包括自身ip,路由地址等
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)
- iOS获取当前无线连接的SSID信息和WI-FI名称
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL
- 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第四节 获取当前TCP连接情况
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十二节 获得本机系统网络接口适配器的列表
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第四节 获取当前TCP连接情况
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十二节 获得本机系统网络接口适配器的列表
- C# .NET 获取网络适配器信息和路径信息