Exception when using FolderBrowserDialog

I had the same issue with ASP.NET MVC project. When I export my crystal report to some format it shows me the error. What I have done is replace

This:

            SaveFileDialog browser = new SaveFileDialog();
            string fileName = "";

            browser.Filter = "Pdf|*.pdf|Txt|.txt";

            if (browser.ShowDialog() == DialogResult.OK)
            {
                ExportFormatType formatType = ExportFormatType.NoFormat;
                switch (browser.FilterIndex)
                {
                    case 2:
                        formatType = ExportFormatType.WordForWindows;
                        break;
                    case 1:
                        formatType = ExportFormatType.PortableDocFormat;
                        break;
                }

                fileName = browser.FileName;
                crReportDocument.ExportToDisk(formatType, fileName);

Into:

Thread thread = new Thread((ThreadStart)(() =>
            {
                SaveFileDialog browser = new SaveFileDialog();
                string fileName = "";

                browser.Filter = "Pdf|*.pdf|Txt|.txt";

                if (browser.ShowDialog() == DialogResult.OK)
                {
                    ExportFormatType formatType = ExportFormatType.NoFormat;
                    switch (browser.FilterIndex)
                    {
                        case 2:
                            formatType = ExportFormatType.WordForWindows;
                            break;
                        case 1:
                            formatType = ExportFormatType.PortableDocFormat;
                            break;
                    }

                    fileName = browser.FileName;
                    crReportDocument.ExportToDisk(formatType, fileName);
                }
            }));

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

A thread is either STA or MTA it can't be specified just for one method so the attribute must be present on the entry point.

From STAThreadAttribute in MSDN :

Apply this attribute to the entry point method (the Main() method in C# and Visual Basic). It has no effect on other methods.

If this code is called from a secondary thread you have 3 choices :

IMPORTANT NOTE: Running (as you seem to do) System.Windows.Forms code inside an MTA thread is unwise, some functionalities like file open dialogs (not only folder) require a MTA thread to work.

Changing your secondary thread apartment

If you create the thread yourself (and don't use the specificity of MTA) you could just change it's apartment before starting it :

var t = new Thread(...);
t.SetApartmentState(ApartmentState.STA);

 

Creating a thread just for it

If you don't control the thread creation you could do it in a temporary thread :

string selectedPath;
var t = new Thread((ThreadStart)(() => {
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
    fbd.ShowNewFolderButton = true;
    if (fbd.ShowDialog() == DialogResult.Cancel)
        return;

    selectedPath = fbd.SelectedPath;
}));

t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
Console.WriteLine(selectedPath);

 

Invoking in another(STA) thread

If your main thread also contain System.Windows.Forms code you could invoke in it's message loop to execute your code :

string selectedPath = null;
Form f = // Some other form created on an STA thread;
f.Invoke(((Action)(() => {
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
    fbd.ShowNewFolderButton = true;
    if (fbd.ShowDialog() == DialogResult.Cancel)
        return;

    selectedPath = fbd.SelectedPath;
})), null);
Console.WriteLine(selectedPath);

As simple as the below :

using System.Windows.Forms;
namespace fileConverterBaset64
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)

Add the command [STAThread] before your main method. That's it, it would work.


This fixed my issue. [STAThread] static void Main()

Just an extra question: why can't microsoft make things simple? Are they trying to disgust people to do some coding?