如何以编程方式获取计算机的本地IP地址? [英] How do I get the Local Network IP address of a computer programmatically?

查看:93
本文介绍了如何以编程方式获取计算机的本地IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C#和.NET 3.5从我的程序中获取计算机的实际本地网络IP地址(例如192.168.0.220)。在这种情况下,我不能只使用127.0.0.1。

I need to get the actual local network IP address of the computer (e.g. 192.168.0.220) from my program using C# and .NET 3.5. I can't just use 127.0.0.1 in this case.

执行此操作的最佳方法是什么?

What's the best way to do this?

推荐答案

链接
它说在那里,添加System.net,并使用以下

Link It says there, add System.net, and using the following

    //To get the local IP address 
string sHostName = Dns.GetHostName (); 
IPHostEntry ipE = Dns.GetHostByName (sHostName); 
IPAddress [] IpA = ipE.AddressList; 
for (int i = 0; i < IpA.Length; i++) 
{ 
    Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ()); 
}

这篇关于如何以编程方式获取计算机的本地IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆