2010-04-20 130 views
1

我创建了一个使用.NET 3.5编译的应用程序。并使用了FolderBrowserDialog对象 。当按下按钮时,我执行以下代码:文件夹浏览对话框没有显示文件夹

FolderBrowserDialog fbd = new FolderBrowserDialog(); 
fbd.ShowDialog(); 

显示文件夹对话框,但我看不到任何文件夹。我只看到 是按钮OK &取消(并且当 ShowNewFolderButton properyty设置为true时创建新文件夹按钮)。当我尝试完全相同的 不同形式的代码时,一切正常。

任何想法??

回答

1

检查运行对话框的线程是否在STAThread上。因此,例如,请确保您的Main方法标有[STAThread]属性:

[STAThread] 
static void Main() { 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new Form1()); 
} 

否则,你可以(在FolderBrowserDialog Class从社区内容)做到这一点。

/// <summary> 
/// Gets the folder in Sta Thread 
/// </summary> 
/// <returns>The path to the selected folder or (if nothing selected) the empty value</returns> 
private static string ChooseFolderHelper() 
{ 
    var result = new StringBuilder(); 
    var thread = new Thread(obj => 
    { 
     var builder = (StringBuilder)obj; 
     using (var dialog = new FolderBrowserDialog()) 
     { 
      dialog.Description = "Specify the directory"; 
      dialog.RootFolder = Environment.SpecialFolder.MyComputer; 
      if (dialog.ShowDialog() == DialogResult.OK) 
      { 
       builder.Append(dialog.SelectedPath); 
      } 
     } 
    }); 

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

    while (thread.IsAlive) 
    { 
     Thread.Sleep(100); 
     } 

    return result.ToString(); 
} 
+0

非常有帮助。谢谢! – chessofnerd 2012-05-29 16:51:48

相关问题