如何在c#中为Windows Mobile 6.0设备编程获取MAC地址

问题描述:

如何在c#中为Windows Mobile 6.0设备以编程方式获取MAC地址? System.Net.NetworkInformation在net compatc framework 3.5中不受支持。如何在c#中为Windows Mobile 6.0设备编程获取MAC地址

本文不是技术概述或大型讨论。这就像一个关于如何获得机器的IP地址或主机名的提示。在Win32 API中,这可以使用NetWork API来完成。这在.NET框架中仍然如此。唯一的区别是找到并理解用于完成此任务的名称空间和类。在.NET框架中,NetWork API在System.Net命名空间中可用。 System.Net命名空间中的DNS类可用于获取计算机的主机名,或者在主机名已知的情况下获取IP地址。 DNS类提供了一个简单的域名解析功能。 DNS类是一个静态类,它提供对来自Internet域名系统(DNS)的信息的访问。如果指定的主机在DNS数据库中有多个条目,则返回的信息包括多个IP地址和别名。该列表以集合或IPAddress对象数组的形式返回。以下部分是显示如何获取给定主机名的IP地址的代码。

namespace NKUtilities 
{ 
    using System; 
    using System.Net; 

    public class DNSUtility 
    { 
     public static int Main (string [] args) 
     { 

      String strHostName = new String (""); 
      if (args.Length == 0) 
      { 
       // Getting Ip address of local machine... 
       // First get the host name of local machine. 
       strHostName = DNS.GetHostName(); 
       Console.WriteLine ("Local Machine's Host Name: " + strHostName); 
      } 
      else 
      { 
       strHostName = args[0]; 
      } 

      // Then using host name, get the IP address list.. 
      IPHostEntry ipEntry = DNS.GetHostByName (strHostName); 
      IPAddress [] addr = ipEntry.AddressList; 

      for (int i = 0; i < addr.Length; i++) 
      { 
       Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString()); 
      } 
      return 0; 
     }  
    } 
} 

http://www.codeproject.com/Articles/854/How-To-Get-IP-Address-Of-A-Machine

+0

该链接看起来已经死亡。 – Sevki 2010-11-11 07:46:48

+0

上面的链接讲述了获取IP地址。我需要MAC地址而不是IP地址。 – Vicky 2010-11-11 10:10:40

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 
//for each j you can get the MAC 
PhysicalAddress address = nics[0].GetPhysicalAddress(); 
byte[] bytes = address.GetAddressBytes(); 
for (int i = 0; i < bytes.Length; i++) 
{ 
    // Display the physical address in hexadecimal. 
    Console.Write("{0}", bytes[i].ToString("X2")); 
    // Insert a hyphen after each byte, unless we are at the end of the 
    // address. 
    if (i != bytes.Length - 1) 
    { 
     Console.Write("-"); 
    } 
} 
+1

根据文档,紧凑框架支持NetworkInterface和PhysicalAddress。 – c0D3l0g1c 2010-11-12 04:29:46

+1

根据这个:http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface_members(v = VS.90).aspx,我看到它在紧凑框架中不受支持。我不确定你指的是哪个文档。 – Vicky 2010-11-25 09:15:50

我们可以使用MDSDK在这种类型的问题,像

using PsionTeklogix.Peripherals; 
namespace EnumAdapters 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      ArrayList pList = null; 
      string[] lStatistic = null; 

      try 
      { 
       pList = Peripherals.EnumerateAdapters(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("failed with\r\n" + ex.Message, "EnumerateAdapters()"); 
       Close(); 
       return; 
      } 

      listBox1.Items.Clear(); 

      foreach (string AdapterName in pList) 
      { 
       try 
       { 
        listBox1.Items.Add(AdapterName + (Peripherals.IsAdapterPresent(AdapterName) ? " is present" : " is NOT present") + (Peripherals.IsWirelessAdapter(AdapterName) ? " [wireless adapter] " : "")); 
        lStatistic = Peripherals.GetAdapterStatistics(AdapterName); // See Note 1 
        foreach (string StatInfo in lStatistic) 
        { 
         if (StatInfo.StartsWith("Local MAC Address")) 
         { 
          listBox1.Items.Add("» " + StatInfo); 
          break; 
         } 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
        Close(); 
        return; 
       } 
      } 
     } 
    } 
} 
+1

这使用了特定于设备的库,因此只能在* Psion设备上工作*。 – ctacke 2012-09-07 12:47:20

我知道它已经一段时间,但我需要这个,发现我可以使用OpenNETCF以上代码版本的调整:

INetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 
//for each j you can get the MAC 
PhysicalAddress address = nics[0].GetPhysicalAddress(); 
byte[] bytes = address.GetAddressBytes(); 
for(int i = 0; i < bytes.Length; i++) { 
    // Display the physical address in hexadecimal. 
    Console.Write("{0}", bytes[i].ToString("X2")); 
    // Insert a hyphen after each byte, unless we are at the end of the address. 
    if(i != bytes.Length - 1) { 
     Console.Write("-"); 
    } 
}