2008-10-20 13 views
16

में नेटवर्क एडाप्टर से पढ़ें MAC पता मैं चाहूँगा पहले सक्रिय नेटवर्क का उपयोग कर VB.net या सी # एक WinForm आवेदननेट

उत्तर

0
के लिए (.NET 3.5 एसपी 1 का प्रयोग करके) एडाप्टर से मैक पते को पढ़ने के लिए सक्षम होने के लिए

आप DllImport GetAdaptersInfo करने की जरूरत है - यहाँ कुछ सी # कोड

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

+0

अरे Lou - वास्तविक कोड पोस्ट करने से पहले आपको मेरे साथ जांच करनी चाहिए! -स्टीव – plinth

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 

से है मुझे यकीन है कि आप करेंगे हा यदि आपको

+0

और क्या खुशी है अगर नोटबुक जहां यह कोड चलता है नेटवर्क एडाप्टर अक्षम है? – Romias

34

की आवश्यकता है, तो .NET 2.0 System.Net.NetworkInformation नामस्थान में नेटवर्क इंटरफेस क्लास रहा है जो आपको यह जानकारी देगा। इस प्रयास करें:

 foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) 
     { 
      if (nic.OperationalStatus == OperationalStatus.Up) 
      { 
       Console.WriteLine(nic.GetPhysicalAddress().ToString()); 
       break; 
      } 
     } 
+0

महान काम करता है। को बदलने की आवश्यकता है यदि (nic.OperationalStatus == OperationalStatus.Up) – blak3r

+0

धन्यवाद यह मेरी मदद करता है! –

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 विधि बेहतर हो सकता है कर रहा हूँ, लेकिन यह नौकरी करता है

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(); 
0

ऐसा लगता है कि यह एक पुरानी पोस्ट है, लेकिन मुझे पता है कि आप इस थ्रेड मदद के लिए इसलिए यहाँ देख में चलेंगे जैसे मैं आज मेरा लैपटॉप में सभी नेटवर्क इंटरफेस की MAC पते पाने के लिए क्या किया है।

सबसे पहले आप आयात करना है निम्नलिखित

Imports System.Net.NetworkInformation 

इस समारोह है कि एक स्ट्रिंग सरणी

Private Function GetMAC() As String() 
    Dim MACAddresses(0) As String 
    Dim i As Integer = 0 
    Dim NIC As NetworkInterface 

    For Each NIC In NetworkInterface.GetAllNetworkInterfaces 
     ReDim Preserve MACAddresses(i) 
     MACAddresses(i) = String.Format("{0}", NIC.GetPhysicalAddress()) 
     i += 1 
    Next 
    Return MACAddresses 
End Function 
0

में सभी MAC पते रिटर्न अधिक सीमित कम्पैक्ट फ्रेमवर्क का उपयोग कर किसी के लिए (है .NET v2.0 CF) निम्न कोड Windows CE 5.0 और CE 6.0 दोनों पर काम करता है (केवल एडाप्टर नाम पढ़ रहा है, लेकिन लौटाई गई संरचना की पूरी परिभाषा प्राप्त करने के लिए एमएसडीएन पर "टाइपपीफ स्ट्रक्चर _IP_ADAPTER_INFO" की खोज करें):

private const int MAX_ADAPTER_NAME_LENGTH = 256; 
[DllImport ("iphlpapi.dll", SetLastError = true)] 
private static extern int GetAdaptersInfo(byte[] abyAdaptor, ref int nSize); 

// ... 
private static string m_szAdaptorName = "DM9CE1"; 

// ... 
private void GetNetworkAdaptorName() 
{ 
    // The initial call is to determine the size of the memory required. This will fail 
    // with the error code "111" which is defined by MSDN to be "ERROR_BUFFER_OVERFLOW". 
    // The structure size should be 640 bytes per adaptor. 
    int nSize = 0; 
    int nReturn = GetAdaptersInfo(null, ref nSize); 

    // Allocate memory and get data 
    byte[] abyAdapatorInfo = new byte[nSize]; 
    nReturn = GetAdaptersInfo(abyAdapatorInfo, ref nSize); 
    if (nReturn == 0) 
    { 
     // Find the start and end bytes of the name in the returned structure 
     int nStartNamePos = 8; 
     int nEndNamePos = 8; 
     while ((abyAdapatorInfo[nEndNamePos] != 0) && 
        ((nEndNamePos - nStartNamePos) < MAX_ADAPTER_NAME_LENGTH)) 
     { 
      // Another character in the name 
      nEndNamePos++; 
     } 

     // Convert the name from a byte array into a string 
     m_szAdaptorName = Encoding.UTF8.GetString(
      abyAdapatorInfo, nStartNamePos, (nEndNamePos - nStartNamePos)); 
    } 
    else 
    { 
     // Failed? Use a hard-coded network adaptor name. 
     m_szAdaptorName = "DM9CE1"; 
    } 
}