-1

I have a tiny problem: How to close the application If the computer does not have the internet connection?

My app is going to do something, but first and before doing its job need to check the internet connection, if there is no internet connection, then close it self.

private void Form1_Load(object sender, EventArgs e)
{

    // here is my code that gonna do something.  before this check the internet connection. if no connection close your self

}
Filburt
  • 17,626
  • 12
  • 64
  • 115
S. miler
  • 23
  • 2
  • Do you really care if there's an internet connection or just that a particular host can be reached? – itsme86 Nov 22 '17 at 15:35
  • 1
    Try to reach something on the internet, and if you get a timeout, assume that you have no connection. I would not _close the form_ by the way. Just give the user an option to retry or to close. I would quickly stop using your application if it closed every time I have an internet hick-up... – oerkelens Nov 22 '17 at 15:36
  • hello. my application needs to the internet connection to doing its job. so its really important. but i need to close my application form if computer does not have a internet connection – S. miler Nov 22 '17 at 15:37
  • 1
    As soon as you've obtained the answer to the question "does this machine have an internet connection?", and *before* you can take any action on it, the actual reality may be the complete opposite of the answer you're now reasoning about. **no** amount of pre-checking can determine whether, at some point in the *immediate* future, you'll be able to complete any *particular* task that requires the internet. – Damien_The_Unbeliever Nov 22 '17 at 15:47

1 Answers1

0

I would use System.Net.NetworkInformation.Ping and ping Google.co.uk. then determining whether there is a time out or not check this out seems to solve your question. However this will only tell you if the port is active, not whether you have connection, so may be a crude fix follow this link How do you check if a website is online in C#?

Edit: another option is by the following: Begin by creating a separate method as per below, within your winform project

public static bool CheckNetwork()
        {
            if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                //Do your stuffs when network available
                return true;

            }
            else
            {
                //When network not available,  
                return false;
            }
        }

Then within your formload Event handler have the following. This action will then be performed when the form is loaded. You can then have what other functionality you want to perform based of the separate method.

private void Form1_Load(object sender, EventArgs e)
        {
            if (!CheckNetwork())
            {
                MessageBox.Show("No connection available");
                this.Close();
            }
            else
            {

                MessageBox.Show("Network connection available");
            }
        }
Chaos
  • 633
  • 1
  • 6
  • 14
  • thank you so much . it was really helpful. but i still cant solve my problem. i only need to exit my application without any error or exp. i mean if there is no internet connection, dont get any errors or exp, just close it self – S. miler Nov 22 '17 at 15:56
  • Create a method that will return boolean based on your connection such as `bool networkUp = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();` and if false close the application by `this.close()`. – Chaos Nov 22 '17 at 16:09
  • thanks dave <3 i got it but i am new to c#... so can you explain me how do i use if method to close my app? i got System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable now how do i use if method to check the result and if no connection close my app? – S. miler Nov 22 '17 at 16:17
  • Edited for you, with a code example if you have any questions just ask and I will do my best to answer. – Chaos Nov 22 '17 at 17:47