0

I have a method that requires internet connection to function, How can I check for internet connection before my form loads? I would like to display an error message box if there is no connection and NOT load to form.

Here's my attempt:

private void Form1_Load(object sender, EventArgs e)
    {

        bool checkConnection = NetworkInterface.GetIsNetworkAvailable();
        if (checkConnection = false)
        {
            MessageBox.Show("error no con.");
        }

1 Answers1

0

Looking at your code

if (checkConnection = false)
{
     MessageBox.Show("error no con.");
}

Notice

if (checkConnection = false)

it's = Not ==

so you should change it and make

if (!checkConnection)

And FYI NetworkInterface.GetIsNetworkAvailable();

is not an efficient way of checking for internet connection.

Please refer this for checking internet connectivity

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28