17

WinFormアプリケーションのVB.netまたはC#(.NET 3.5 SP1を使用)を使用して、最初のアクティブなネットワークアダプターからMACアドレスを読み取れるようにしたいと思います。

4

7 に答える 7

34

.Net 2.0以降、System.Net.NetworkInformation名前空間にこの情報を提供するNetworkInterfaceクラスがあります。これを試して:

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                Console.WriteLine(nic.GetPhysicalAddress().ToString());
                break;
            }
        }
于 2008-10-20T13:00:36.703 に答える
4

http://www.dotnetjunkies.com/WebLog/jkirwan/archive/2004/02/10/6943.aspxから

  Dim mc As System.Management.ManagementClass
  Dim mo As ManagementObject
  mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
  Dim moc As ManagementObjectCollection = mc.GetInstances()
  For Each mo In moc
     If mo.Item("IPEnabled") = True Then
        ListBox1.Items.Add("MAC address " & mo.Item("MacAddress").ToString())
     End If
  Next

必要に応じて、このコードをC#に移植するのに問題はないと確信しています。

于 2008-10-20T12:50:14.400 に答える
3
using Linq..

using System.Net.NetworkInformation;
..

NetworkInterface nic =
    NetworkInterface.GetAllNetworkInterfaces()
    .Where(n => n.OperationalStatus == OperationalStatus.Up).FirstOrDefault();

if (nic != null)
    return nic.GetPhysicalAddress().ToString();
于 2011-04-27T11:05:50.580 に答える
2

これを行うクラスは次のとおりです。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MacAddress
{
    class MacAddress
    {
        byte[] _address;

        public MacAddress(byte[] b)
        {
            if (b == null)
                throw new ArgumentNullException("b");
            if (b.Length != 8)
                throw new ArgumentOutOfRangeException("b");
            _address = new byte[b.Length];
            Array.Copy(b, _address, b.Length);
        }

        public byte[] Address { get { return _address; } }

        public override string ToString()
        {
            return Address[0].ToString("X2", System.Globalization.CultureInfo.InvariantCulture) + ":" +
                    Address[1].ToString("X2", System.Globalization.CultureInfo.InvariantCulture) + ":" +
                    Address[2].ToString("X2", System.Globalization.CultureInfo.InvariantCulture) + ":" +
                    Address[3].ToString("X2", System.Globalization.CultureInfo.InvariantCulture) + ":" +
                    Address[4].ToString("X2", System.Globalization.CultureInfo.InvariantCulture) + ":" +
                    Address[5].ToString("X2", System.Globalization.CultureInfo.InvariantCulture);
        }

        public static List<MacAddress> GetMacAddresses()
        {
            int size = 0;
            // this chunk of code teases out the first adapter info
            int r = GetAdaptersInfo(null, ref size);
            if ((r != IPConfigConst.ERROR_SUCCESS) && (r != IPConfigConst.ERROR_BUFFER_OVERFLOW))
            {
                return null;
            }
            Byte[] buffer = new Byte[size];
            r = GetAdaptersInfo(buffer, ref size);
            if (r != IPConfigConst.ERROR_SUCCESS)
            {
                return null;
            }
            AdapterInfo Adapter = new AdapterInfo();
            ByteArray_To_IPAdapterInfo(ref Adapter, buffer, Marshal.SizeOf(Adapter));

            List<MacAddress> addresses = new List<MacAddress>();
            do
            {
                addresses.Add(new MacAddress(Adapter.Address));
                IntPtr p = Adapter.NextPointer;
                if (p != IntPtr.Zero)
                {
                    IntPtr_To_IPAdapterInfo(ref Adapter, p, Marshal.SizeOf(Adapter));
                }
                else
                {
                    break;
                }
            } while (true);
            return addresses;
        }

        // glue definitions into windows
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        private struct IPAddrString
        {
            public IntPtr NextPointer;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4 * 4)]
            public String IPAddressString;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4 * 4)]
            public String IPMaskString;
            public int Context;
        }

        private class IPConfigConst
        {
            public const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
            public const int MAX_ADAPTER_NAME_LENGTH = 256;
            public const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
            public const int ERROR_BUFFER_OVERFLOW = 111;
            public const int ERROR_SUCCESS = 0;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        private struct AdapterInfo
        {
            public IntPtr NextPointer;
            public int ComboIndex;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = IPConfigConst.MAX_ADAPTER_NAME_LENGTH + 4)]
            public string AdapterName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = IPConfigConst.MAX_ADAPTER_DESCRIPTION_LENGTH + 4)]
            public string Description;
            public int AddressLength;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = IPConfigConst.MAX_ADAPTER_ADDRESS_LENGTH)]
            public Byte[] Address;
            public int Index;
            public int Type;
            public int DhcpEnabled;
            public IntPtr CurrentIPAddress;
            public IPAddrString IPAddressList;
            public IPAddrString GatewayList;
            public IPAddrString DhcpServer;
            public Boolean HaveWins;
            public IPAddrString PrimaryWinsServer;
            public IPAddrString SecondaryWinsServer;
            public int LeaseObtained;
            public int LeaseExpires;
        }
        [DllImport("Iphlpapi.dll", CharSet = CharSet.Auto)]
        private static extern int GetAdaptersInfo(Byte[] PAdapterInfoBuffer, ref int size);
        [DllImport("Kernel32.dll", EntryPoint = "CopyMemory")]
        private static extern void ByteArray_To_IPAdapterInfo(ref AdapterInfo dst, Byte[] src, int size);
        [DllImport("Kernel32.dll", EntryPoint = "CopyMemory")]
        private static extern void IntPtr_To_IPAdapterInfo(ref AdapterInfo dst, IntPtr src, int size);
    }
}

そして、ここにいくつかのテストコードがあります:

        List<MacAddress> addresses = MacAddress.GetMacAddresses();
        foreach (MacAddress address in addresses)
        {
            Console.WriteLine(address);
        }

ToString メソッドの方が優れていると確信していますが、それで十分です。

于 2008-10-20T13:33:36.667 に答える
0

GetAdaptersInfoをDllImportする必要があります-ここにいくつかのC#コードがあります

http://www.codeguru.com/cpp/in/network/networkinformation/comments.php/c5451/?thread=60212

于 2008-10-20T12:45:23.020 に答える