2010-11-10 31 views

回答

0

本文不是技術概述或大型討論。這就像一個關於如何獲得機器的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

0
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

0

我們可以使用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

3

我知道它已經一段時間,但我需要這個,發現我可以使用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("-"); 
    } 
} 
相關問題