我怎么知道我的电脑没有连接到网络? [英] How do I know if my computer's not connected to a network?

查看:161
本文介绍了我怎么知道我的电脑没有连接到网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个局域网聊天应用程序(服务器和客户端程序),我想知道我能得到我自己的计算机的IP地址(用于服务器程序),并发现的这个问题

I'm making a LAN chat application (server and client program) and I wanted to know how can I get my own computer's IP address (for the server program) and found this question.

我用了代码在我的程序和测试它,而我的电脑没有连接到互联网,到局域网或任何外部设备,我还以为代码将抛出一个异常,因为这(和将意味着我的电脑没有连接任何网络).. 。但事与愿违,反而,它返回一个IP地址。

I used the code in my program and tested it while my computer's not connected to the internet, to a LAN or any external devices and I thought that the code will throw an exception because of that (and would mean that my computer's not connected any network)... but it didn't and instead, it returned an IP Address.

有没有办法找出如果我的电脑没有连接到任何网络?

Is there a way to find out if my computer's not connected to any network?

推荐答案

通过System.Net.NetworkInformation可以收集有关您的网络接口信息。因此,这可以帮助你:

With System.Net.NetworkInformation you can gather informations about your Network interfaces. So this should help you:

public Boolean isConnected()
{
    NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface face in interfaces)
    {
        if (face.OperationalStatus == OperationalStatus.Up || face.OperationalStatus == OperationalStatus.Unknown)
        {
            // Internal network interfaces from VM adapters can still be connected 
            IPv4InterfaceStatistics statistics = face.GetIPv4Statistics();
            if (statistics.BytesReceived > 0 && statistics.BytesSent > 0)
            {
                // A network interface is up
                return true;
            }
        }
    }
    // No Interfaces are up
    return false;
}

这篇关于我怎么知道我的电脑没有连接到网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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