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();
}
}
}
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! –
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
Probabilmente dovresti includere un link alla pagina da cui hai copiato questo codice, non credi? Voglio dire, è uno dei primi risultati su Google. – Kevin