您的位置:首页 > 理论基础 > 计算机网络

获取网络适配器的相关信息(包括网络连接名称)

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐