Wednesday, October 8, 2008

โค้ด C# ดึงข้อมูล IP และ Hostname

Get local ip and hostname with C#.

Ref : codeproject.com

  
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
using System.Collections;

public class ....
{
// Get local IP
public static string GetIP()
{
string ip = "";

string strHostName = GetHostName();

//IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);

IPAddress[] addr = ipEntry.AddressList;
ip = addr[0].ToString();

//MessageBox.Show(ip);

return ip;
}


// Get hostname
public static string GetHostName()
{
return Dns.GetHostName();
}
}

No comments:

Post a Comment

Popular Posts