如何退出Windows窗体应用程序? [英] How Do I Exit a Windows Form Application?

查看:242
本文介绍了如何退出Windows窗体应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序,它在目录中搜索是否存在文件。如果文件不存在,我想完全关闭应用程序。



我尝试使用我创建的名为Init的函数中的Application.Exit() (),搜索有问题的文件。我收到一条错误消息ObjectDisposedExcetion未处理。



我用Close()调用替换了Application.Exit(),但得到了相同的结果错误消息。



这是我的代码:

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:

        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();
                

            }
        }

推荐答案

请调试您的代码。 Beacuse Windows表单将与application.exit关闭。此行之前有一些错误。尝试Exception e而不是DirectoryNotFoundException e。这应解决您的问题。



表格将出现一次。然后在文件上传事件后,您的Application.Exit()将起作用。它在第一次加载表单时不起作用。
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.


application.Exit();


这篇关于如何退出Windows窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆