文件夹浏览对话框未显示文件夹 [英] Folder browse dialog not showing folders
问题描述
我创建了一个用.NET 3.5编译的应用程序.并使用了 FolderBrowserDialog对象.当按下按钮时,我执行以下代码:
I have created an application compiled with .NET 3.5. and used the FolderBrowserDialog object. When a button is pressed I execute this code:
FolderBrowserDialog fbd = new FolderBrowserDialog ();
fbd.ShowDialog();
显示了文件夹"对话框,但看不到任何文件夹.我唯一看到的是 确定和按钮是什么?取消(并在创建新文件夹按钮时 ShowNewFolderButton属性设置为true).当我尝试完全一样 以另一种形式编写代码,一切工作正常.
A Folder dialog is shown but I can't see any folders. The only thing I see are the buttons OK & Cancel (and create new folder button when the ShowNewFolderButton properyty is set to true). When I try the exact same code on a different form everything is working fine.
任何想法?
推荐答案
检查运行对话框的线程是否在STAThread上.因此,例如,确保您的Main方法带有[STAThread]属性标记:
Check that the thread running your dialog is on an STAThread. So for example make sure your Main method is marked with the [STAThread] attribute:
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
否则,您可以执行此操作(来自 FolderBrowserDialog类).
Otherwise you can do this (from the Community Content on 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();
}
这篇关于文件夹浏览对话框未显示文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!