Hi,
my app needs to have an internet connection to run correctly, so I have been trying to implement a piece of code to check for the availability of a net connection.
private void Form1_Load(object sender, EventArgs e)
{
HttpWebRequest req;
HttpWebResponse resp;
try
{
req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode.ToString().Equals("OK"))
{
MessageBox.Show("its connected.");
}
else
{
MessageBox.Show("its not connected.");
Application.Exit();
}
}
catch (Exception exc)
{
//Console.WriteLine("its not connected.");
}}
as you can see this is only basic for testing purposes.
When I run my app with a net connection the message box pops up to say 'it's connected', as it should and the program will carry on.
However, if I turn my net connection off and then run my app, it does not show the 'it's not connected' message and then Exit as it is supposed to do, instead it just runs the app and my main form appears.
Any help or other ways to code this would be welcome.
Kind regards..,
MT ;)