如何在窗体应用程序中获取系统IP地址? [英] How to get system Ip address in window form application?

查看:135
本文介绍了如何在窗体应用程序中获取系统IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得安装了我的窗口应用程序的系统IP地址...是否可以在窗口表单应用程序中使用?



smeone help我是个好主意...



先谢谢..

Hi,I want to get system IP address in which my window application is installed...can it be possible in window form application??

smeone help me plzz...

Thanks in advance..

推荐答案

取决于你以后的IP地址。

如果你想在局域网上使用你的电脑的IP地址,那就很容易了:检索LAN的IP和MAC地址 [ ^ ]覆盖它,只需使用 GetIPAddress 方法并忽略其余部分。



如果你想要通过互联网向外界提供你的IP地址,那就更复杂了 - 它不是与您的PC有关,但与您的路由器有关,并且根本没有进入局域网。因此,您必须向外部服务询问价值: http://www.whatsmyip.org/ [ ^ ]是您可以提出的一种服务。
Depends which IP address you are after.
If you want the IP address of your PC on the LAN, then that is pretty easy: Retrieving IP and MAC addresses for a LAN[^] covers it, just use the GetIPAddress method and ignore the rest.

If you want your IP address that you present to the outside world via teh internet, then that is more complex - it isn''t related to your PC, but to your Router and doesn''t come into the LAN at all. So you have to ask an external service for the value: http://www.whatsmyip.org/[^] is one such service you can ask.


什么编程语言使用。



C#.Net示例

尝试此功能获取您的公共IP

What programming language do use.

Example for C#.Net
Try this function to get your public IP
public string GetPublicIP()
{
    String direction = "";
    WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
    using (WebResponse response = request.GetResponse())
    using (StreamReader stream = new StreamReader(response.GetResponseStream()))
    {
        direction = stream.ReadToEnd();
    }

    //Search for the ip in the html
    int first = direction.IndexOf("Address: ") + 9;
    int last = direction.LastIndexOf("</body>");
    direction = direction.Substring(first, last - first);

    return direction;
}





并尝试使用此功能获取您的本地IP。





and try this function to get your local IP.

public string GetLocalIP(){
return Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork) ?? new IPAddress( new byte[] {127, 0, 0, 1} );
}





希望这会有所帮助。



Hope this helps.


这篇关于如何在窗体应用程序中获取系统IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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