hi im amir
im a noob in c# and need some help

i coding a net based app and i need to ping to website if ok run app else close app
the bug is when im online show ok message but when im offline app runnig and show error for debugging.

please help
thankyou

here is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
////////////////////////////////////
using System.Net.NetworkInformation;
////////////////////////////////////

namespace CheckInternetConnection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Ping ping = new Ping();
            PingReply pingStatus = ping.Send("google.com");
            if (pingStatus.Status == IPStatus.Unknown)
            {
                Application.Exit();
            }
            else
            {
                ImgStatus.Visible = true;
                lblStatus.Visible = true;
                ImgStatus.BackgroundImage = CheckInternetConnection.Properties.Resources.Actions_dialog_ok_apply_icon;
                lblStatus.Text = "OK network active";
                lblStatus.ForeColor = Color.FromArgb(0, 192, 0);
            }
        }
    }
}
PingReply pingStatus = ping.Send("google.com");

When you are offline it can't resolve the name "google.com" thus you get an exception.

PingReply pingStatus = ping.Send("google.com");

When you are offline it can't resolve the name "google.com" thus you get an exception.

yes i get error app dont exit still running
how to fix or recode it when offline app dont start
thanks

private void Form1_Load(object sender, EventArgs e) {
    try {
        Ping ping = new Ping();
        PingReply pingStatus = ping.Send("google.com");
        if (pingStatus.Status == IPStatus.Unknown) {
            Application.Exit();
        } else {
            ImgStatus.Visible = true;
            lblStatus.Visible = true;
            ImgStatus.BackgroundImage = CheckInternetConnection.Properties.Resources.Actions_dialog_ok_apply_icon;
            lblStatus.Text = "OK network active";
            lblStatus.ForeColor = Color.FromArgb(0, 192, 0);
        }
    } catch (Exception e) {
        Application.Exit(0);
    }
}
private void Form1_Load(object sender, EventArgs e) {
    try {
        Ping ping = new Ping();
        PingReply pingStatus = ping.Send("google.com");
        if (pingStatus.Status == IPStatus.Unknown) {
            Application.Exit();
        } else {
            ImgStatus.Visible = true;
            lblStatus.Visible = true;
            ImgStatus.BackgroundImage = CheckInternetConnection.Properties.Resources.Actions_dialog_ok_apply_icon;
            lblStatus.Text = "OK network active";
            lblStatus.ForeColor = Color.FromArgb(0, 192, 0);
        }
    } catch (Exception e) {
        Application.Exit(0);
    }
}

thankyou my friend
i tryed this but have errors in

catch (Exception e) {
        Application.Exit(0);
    }

and i changed to

catch (Exception q) {
        Application.Exit();
    }

works fine

Sorry, been programming in other languages that require exit codes :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.