3

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?

Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
killie01
  • 253
  • 3
  • 11
  • 1
    "im using the code that worked before for me, so it CAN NOT be the code"... I'd learn to be less complacent about your code. Most errors are due to "the code". If you're sure it's not your code, you're posting in the wrong place! – spender Jan 17 '12 at 15:20
  • There will be more info about the nature of the crash in the windows application event logs. You'll need to provide it. – spender Jan 17 '12 at 15:23
  • 1
    It doesn't catch any exception in catch (Exception ex) {}? – pistipanko Jan 17 '12 at 15:23
  • Are you running this from Visual Studio? Have you tried runing it as Administrator - I've come across some funnies that way with Windows 7. – ChrisBD Jan 17 '12 at 16:20
  • 1
    And while we're at it, `FolderBrowserDialog` implements `IDisposable`, so your lifetime of `fbd` should be in a `using` construct as such `using (FolderBrowserDialog fbd = new FolderBrowserDialog()) { ... }` – Jesse C. Slicer Feb 27 '17 at 14:32
  • Is this code running in the UI thread? If not, it might be an idea to ensure that it is. – spender Jan 17 '12 at 15:21

4 Answers4

4

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);
            }                
        };
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
3

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();
        }
    }
}
Community
  • 1
  • 1
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • Thank you! I couldn't have spotted the problem without this answer. In my case calling `FolderBrowserDialog` would get stuck indefinitely and do nothing. – Jani Aug 01 '15 at 16:46
0

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.

Alexandru Dicu
  • 1,151
  • 1
  • 16
  • 24
0

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.

Daniel
  • 1