打开FolderBrowser时显示异常

时间:2014-06-17 13:03:40

标签: c#-4.0

我想选择路径,所以我使用的是Folderbrowser对话框。但它在“DialogResult result = fbd.ShowDialog();”行中显示异常,“例外是

      Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

public void OpenFolder()   {

        FolderBrowserDialog fbd = new FolderBrowserDialog();
         DialogResult result = fbd.ShowDialog();
        if (result == DialogResult.OK)
        {

            Folderpath = fbd.SelectedPath;

        }

}

1 个答案:

答案 0 :(得分:0)

在主方法

上方添加[STAThreadAttribute]属性
[STAThreadAttribute]
    static void Main(string[] args)
    {
        OpenFolder();
    }