At startup of a Windows Forms application I read a XML file. If this file doesn’t exist or doesn’t have a certain setting I want to display a MessageBox to the user indicating the problem and then close the application.
I tried to do that in the Load event of the main form with Application.Exit() like so:
private void MainForm_Load(object sender, EventArgs e)
{
if (!StartupSettingsCorrect())
{
MessageBox.Show("Blabla... Can't start application.");
Application.Exit();
}
// Other stuff...
}
But this doesn’t seem to be clean. (The application is running but “invisible” with no form on the screen.)
What is instead the best way and place for a clean shutdown in this situation?
Thank you for help in advance!
Environment.Exit()should do the trick. At least it worked for me in the situation you described.According to MSDN, this method