FolderBrowserDialog crashes the application

久未见 提交于 2020-01-04 07:01:28

问题


Whenever I call folderbrowserdialog.showDialog() my application crashes. I'm using the code that worked before for me, so it CAN NOT be the code.

try
{
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    fbd.RootFolder = Environment.SpecialFolder.Desktop;
    if (fbd.ShowDialog() == DialogResult.OK)
    {
        //  this.Minecraft.Text = fbd.SelectedPath;
    }
}
catch
{
}

It does not throw any error, no exception, there just pops up the little loading circle, then the app is gone, I noticed it with a different .NET app before too!

btw: will reinstalling .net 4 work?


回答1:


Try adding this to your application (at the start of the Main() method, preferably). See if the exceptions.txt file has any exceptions logged into it when you reach your freezing point.

        AppDomain.CurrentDomain.FirstChanceException += (sender, e) =>
        {
            if ((e == null) || (e.Exception == null))
            {
                return;
            }

            using (var sw = File.AppendText(@".\exceptions.txt")) 
            {
                sw.WriteLine(e.ExceptionObject);
            }                
        };

        AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
        {
            if ((e == null) || (e.ExceptionObject == null))
            {
                return;
            }

            using (var sw = File.AppendText(@".\exceptions.txt")) 
            {
                sw.WriteLine(e.ExceptionObject);
            }                
        };



回答2:


I had just the same problem with FolderBrowserDialog and found the source of evilness. Comment / uncomment [STAThread] and see the difference:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        //[STAThread]
        static void Main()
        {
            new FolderBrowserDialog().ShowDialog();
        }
    }
}



回答3:


Another thing that you should know about FolderBrowserDialog, SaveFileDialog, OpenFileDialog is that they don't work if you "Disable visual themes" on the compatibility tab from the executable file properties.




回答4:


Hope this helps somebody - i actually had this problem, and turns out I had accidentally assigned a DialogResult to the button that was launching by FolderBrowserDialog! Therefore, whenever the code was finished executing, it was returning the DialogResult of 'Cancel' to the CLR and terminating my program. Check the 'DialogResult' property in Visual Studio for the button you have assigned to open the dialog - make sure it is set to None.



来源:https://stackoverflow.com/questions/8897076/folderbrowserdialog-crashes-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!