Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Windows Form Application that searches a directory for the existence of a file. If the file doesn't exist, I want to close the Application completely.

I tried using "Application.Exit()" from a function I created called "Init()", which searches for the file in question. I got an error message "ObjectDisposedExcetion was unhandled".

I replaced the "Application.Exit()" with a "Close()" call, but got the same error message.

Here's my code:
C#
        private void Init()
        {
            bValidFile = true;
            strDataFileName = "aPIP.dat";
            strDataPath = Directory.GetCurrentDirectory() + "\\Data";
            MessageBox.Show(strDataPath);
            try
            {
                Directory.SetCurrentDirectory(strDataPath);
            }
            catch(DirectoryNotFoundException e)
            {
                MessageBox.Show("***ERROR***\nFile Not Found");
                bValidFile = false;                
            }
            if(!bValidFile)
            {
                Close();
//Application.Exit();
                

            }
        }
Posted
Updated 23-Nov-15 8:16am
v2

Please Debug your code. Beacuse Windows form will close with application.exit.There is some error before this line. try Exception e instead of DirectoryNotFoundException e. This should resolve your issue.

Form will appear once .then after event of file upload your Application.Exit() will work. It will not work at first time load of form.
 
Share this answer
 
v3
Comments
Glen Treankler 23-Nov-15 14:14pm    
Ah! Yes, I see that.

I fixed the problem by putting my logic test in the Main Form's FormActivated() Event.

Many thanks for the help!
application.Exit();
 
Share this answer
 
Comments
Member 11053684 23-Nov-15 13:37pm    
It will not work first load of form.
provi 777 23-Nov-15 13:43pm    
when you want to exit applcation can u give some more details
Glen Treankler 23-Nov-15 13:52pm    
That's the problem, Application.Exit() doesn't kill the App...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900