DialogResult dialogResult = MessageBox.Show("Are you sure, you want to exit ? ", "Application Name", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
if (Application.OpenForms.Count == 0)
Application.Exit();
}
else if (dialogResult == DialogResult.No)
{
e.Cancel = true;
}