MVA 範例
namespace MVATwentyQuestions
{
class Program
{
static void Main(string[] args)
{
GetPrivateIP();
string publicIP = GetPublicIP();
Console.WriteLine("Public IP is: {0}", publicIP);
}
}
static string GetPublicIP()
{
String address = "";
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
address = stream.ReadToEnd();
}
//Search for the ip in the html
int first = address.IndexOf("Address: ") + 9;
int last = address.LastIndexOf("</body> ");
address = address.Substring(first, last - first);
return address;
}
static void GetPrivateIP()
{
foreach (var interfaces in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (var address in interfaces.GetIPProperties().UnicastAddresses)
{
if (address.Address.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine("IP Address: " + address.Address.ToString());
}
}
}
}
}
搜尋網路文章來了解這個主題時,發現有 GetHostByName() 這個過時方法可以使用,但實際在 VS 上使用卻會出現提示'System.Net.Dns.GetHostByName(string)' 已過時:GetHostByName is obsoleted for this typeof, please use GetHostEntry instead
![[C#] IP address-1](https://farm6.staticflickr.com/5489/14586567076_5ca4919d8f_o.jpg)
沒有留言:
張貼留言