2010-11-10 13 views

risposta

0

Questo articolo non è una panoramica tecnica o una discussione più ampia. È come una raccolta di suggerimenti su come ottenere l'indirizzo IP o il nome host di una macchina. Nell'API Win32 questo potrebbe essere realizzato utilizzando l'API NetWork. E questo è ancora vero nel framework .NET. L'unica differenza è trovare e capire quale spazio dei nomi e classe utilizzare per svolgere questa attività. Nel framework .NET l'API NetWork è disponibile nello spazio dei nomi System.Net. La classe DNS nello spazio dei nomi System.Net può essere utilizzata per ottenere il nome host di una macchina o ottenere l'indirizzo IP se il nome host è già noto. La classe DNS fornisce una semplice funzionalità di risoluzione dei nomi di dominio. La classe DNS è una classe statica che fornisce l'accesso alle informazioni da Internet Domain Name System (DNS). Le informazioni restituite includono più indirizzi IP e alias se l'host specificato ha più di una voce nel database DNS. L'elenco viene restituito come raccolta o array di oggetti IPAddress. La seguente sezione è il codice che mostra come ottenere l'indirizzo IP per un determinato nome host.

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

che il collegamento sembra essere morto. – Sevki

+0

Il link sopra parla di ottenere l'indirizzo IP. Ho bisogno dell'indirizzo MAC e non dell'indirizzo IP. – 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

Secondo i documenti, sia NetworkInterface che PhysicalAddress sono supportati dal framework compatto. – c0D3l0g1c

+1

In questo modo: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface_members(v=VS.90).aspx, vedo che non è supportato nel framework compatto. Non sono sicuro dei documenti a cui ti stai riferendo. – Vicky

0

Possiamo usare MDSDK in questo tipo di problemi come

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

Utilizza una libreria specifica per dispositivo e pertanto funzionerà * only * con il dispositivo Psion. – ctacke

3

Lo so che è stato un po ', ma ho bisogno di questo e ho trovato ho potuto usare OpenNETCF versione del codice sopra con un tweak:

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("-"); 
    } 
} 
Problemi correlati