2009-05-13 3 views
16

Sto cercando di capire qual è il mio indirizzo IP da un'applicazione console.Ottieni l'indirizzo IP in un'applicazione console

Sono abituato a un'applicazione Web utilizzando la raccolta Request.ServerVariables e/o Request.UserHostAddress.

Come è possibile farlo in un'app console?

risposta

23

Il modo più semplice per farlo è la seguente:

using System; 
using System.Net; 


namespace ConsoleTest 
{ 
    class Program 
    { 
     static void Main() 
     { 
      String strHostName = string.Empty; 
      // 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); 
      // Then using host name, get the IP address list.. 
      IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); 
      IPAddress[] addr = ipEntry.AddressList; 

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

OK, quindi vedo molte risposte qui, ma questo sembra facile da leggere. Mi piace quello che ha detto Martin Peck sull'avere più indirizzi IP, e penso che questo qui mi dia la soluzione giusta. L'ho eseguito localmente e mi ha dato quello che volevo. Grazie mille! –

+1

Sì, sono d'accordo con Martin, devi fare attenzione a più indirizzi IP. Questo codice gestirà questo e potrai scegliere cosa fare con esso da lì. – CodeLikeBeaker

+2

Probabilmente dovresti includere un link alla pagina da cui hai copiato questo codice, non credi? Voglio dire, è uno dei primi risultati su Google. – Kevin

1
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; 
    }  
} 

fonte: http://www.codeproject.com/KB/cs/network.aspx

1

System.Net.Dns.GetHostAddresses() dovrebbe farlo.

2

Lo spazio dei nomi System.Net è tuo amico qui. In particolare, API come DNS.GetHostByName.

Tuttavia, qualsiasi macchina data può avere più indirizzi IP (più schede NIC, IPv4 e IPv6 ecc.) Quindi non è una domanda altrettanto semplice.

+0

Mi piace molto il tuo commento per avere più indirizzi IP. Sulla base di ciò il codice sopra funzionava molto bene. Grazie! –

2

Prova questo:

String strHostName = Dns.GetHostName(); 

Console.WriteLine("Host Name: " + strHostName); 

// Find host by name IPHostEntry 
iphostentry = Dns.GetHostByName(strHostName); 

// Enumerate IP addresses 
int nIP = 0; 
foreach(IPAddress ipaddress in iphostentry.AddressList) { 
    Console.WriteLine("IP #" + ++nIP + ": " + ipaddress.ToString());  
} 
3

IPAddress [] = addresslist Dns.GetHostAddresses (Dns.GetHostName());