2010-11-10 22 views

cevap

0

Bu makale teknik bir bakış veya büyük tartışma değildir. Bir makinenin IP adresini veya ana bilgisayar adını nasıl alabileceğinize dair bir ipucu koleksiyonu gibidir. Win32 API'sinde bu, NetWork API kullanılarak gerçekleştirilebilir. Ve bu hala .NET çerçevesinde doğrudur. Tek fark, bu görevi yerine getirmek için hangi ad alanının ve sınıfın kullanıldığını bulmak ve anlamaktır. .NET çerçevesinde NetWork API, System.Net ad alanında kullanılabilir. System.Net ad alanındaki DNS sınıfı, bir makinenin ana makine adını almak veya ana makine adı zaten biliniyorsa IP adresini almak için kullanılabilir. DNS sınıfı, basit bir etki alanı adı çözümleme işlevi sağlar. DNS sınıfı, Internet Etki Alanı Adı Sistemi'nden (DNS) bilgiye erişim sağlayan statik bir sınıftır. Belirtilen ana bilgisayar DNS veritabanında birden çok girdiye sahipse, döndürülen bilgiler birden fazla IP adresi ve takma ad içerir. Liste bir koleksiyon veya IPAddress nesnelerinin bir dizisi olarak döndürülür. Aşağıdaki bölüm, belirli bir ana bilgisayar adı için IP adresinin nasıl alınacağını gösteren koddur.

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

Bu bağlantının ölü olduğu anlaşılıyor. – Sevki

+0

Yukarıdaki bağlantı IP adresinin alınmasından bahseder. IP adresine değil MAC adresine ihtiyacım var. – Vicky

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

Dokümanlara göre, hem NetworkInterface hem de PhysicalAddress, kompakt çerçeve tarafından desteklenir. – c0D3l0g1c

+1

Buna göre: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface_members(v=VS.90).aspx, Kompakt çerçevede desteklenmediğini görüyorum. Hangi belgelere başvurduğunuzu bilmiyorum .. – Vicky

0

Biz bunu bir süre oldu biliyorum

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

Bu, aygıta özel bir kitaplık kullanıyor ve bu nedenle Psion aygıtı ile * çalışacaktır *. – ctacke

3

gibi sorunlar bu tip MDSDK kullanabilirsiniz, ama bu gerekli ve ben OpenNETCF kullanabilirsiniz bulundu Yukarıdaki kodun bir düzeltmeyle sürümü:

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("-"); 
    } 
} 
İlgili konular