Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close the application before the form loads

Tags:

c#

I have a program wherein I will check if a file exists. If it does, the form will load. But if not, a messagebox will appear to inform the user, and then the application needs to close without showing the form.

How do I do this properly? I tried using this code on the constructor:

    Environment.Exit(-1);

It does what I want, but from what I've read it's not a good way to do it. Is this correct? Or shall I just go with using the above code.

like image 443
klce Avatar asked Jan 31 '13 08:01

klce


1 Answers

You don't need to call anything if you put your check before the application run of the main form

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        // Check for file 
        if(!File.Exists("your file to check"))             
        {
            MessageBox.Show(.....)
        }
        else
        {
            Application.Run(new frmMain());
        }
    }
like image 134
Steve Avatar answered Oct 29 '22 15:10

Steve