如何知道我的计算机是否已连接到网络 [英] How can I know if my computer is connected to a network

查看:67
本文介绍了如何知道我的计算机是否已连接到网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要从远程计算机访问数据库的项目,我想在尝试打开我的数据库连接之前检查客户端计算机是否连接到网络



我尝试了什么:



之前我用try catch来处理它,但它非常慢,有时会提高一个未处理的异常会提示我调试或关闭程序。

我之前的代码如下所示:



I am working on a project that requires accessing database from a remote computer and i want to check on the lient computer if it is connected to the network before attempting to open my database connection

What I have tried:

I used try catch to handle it before but it is extremely slow and sometimes raise an unhandled exception which will prompt me to either debug or close the program.
My previous code is shown below:

private void CheckNetwork_Tick()
{
try
{
if(con.State !=ConnectionState.Open)
{
con.Open();
}
}
catch(SqlException ex)
{
MessageBox.Show("Computer not connected to network");
}
}

推荐答案

1)要检查您可以使用的网络连接。



1) To check network connection you can use.

bool networkUp
    = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();





返回 true / false







2)检查互联网连接





That will return true/false

OR

2) Check internet connection

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
        using (var stream = client.OpenRead("http://www.google.com"))
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}





如果您对此有任何疑问或疑虑,请与我们联系。



Let me know if you have any query or concern for same.


这篇关于如何知道我的计算机是否已连接到网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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