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

来源:互联网 发布:恶魔的奶爸扒皮 知乎 编辑:程序博客网 时间:2024/06/02 21:07
<script type="text/javascript"><!--google_ad_client = "pub-1522036253440599";google_ad_slot = "1709324679";google_ad_width = 250;google_ad_height = 250;//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

 

 

目前获取网络连接信息的方法有很多种,我用的是wmi方法来获取,但却无法获得网络连接的名称(如本地连接),在网上找了找,终于找到一种不同的实现方式,特此与大家共享:

来源:CodeProject

Sample Image - AdapterInfo.jpg

    [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;

 

 

原创粉丝点击