Yazar: Mustafa Turan, 2009-01-31 06:08:53
Kullanıcı bilgisayarının IP adresini bulmak C# ve .NET altyapısıyla ile mümkün bunun için yazdığım konsol uygulaması:
private string check_ip_address()
{
// ** Kodlayan: Mustafa Turan
// ** http://mustafaturan.net/ && http://www.ipogren.com/
// ** Aktif ip adresini bulalım
string active_ip4_address = "";
IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress s in IPHost.AddressList)
{
try
{
active_ip4_address = s.ToString();
break;
}
catch (Exception ex)
{
continue;
}
}
if (validate_ip(active_ip4_address) == false || active_ip4_address == "127.0.0.1") return "127.0.0.1";
return active_ip4_address; // ** Connection is ok
}
private bool validate_ip(string active_ip4_address)
{// Ip doğrulama kodu
// ** Regular expression check for IP V4
Regex r = new Regex(@"([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})");
Match m = r.Match(active_ip4_address);
return m.Success;
}
// header using System.Net;
Yapmanız gereken check_ip_address() fonksiyonunu her hangi bir stringe atamak sonra dilediğiniz şekilde ekrana bastırabilirsiniz.
// örnek konsol string ip_adresi = check_ip_address(); Console.Writeline(ip_adresi); // ########################################### // // örnek form uygulaması string ip_adresi = check_ip_address(); MessageBox.Show(ip_adresi);
Bu site çalışması, içerik ve yazılar
Creative Commons Attribution 3.0 License (lisansı) ile korunmaktadır.