文件夹浏览对话框不显示文件夹

发布于 2024-08-29 20:31:10 字数 296 浏览 2 评论 0原文

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

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

显示文件夹对话框,但我看不到任何文件夹。我唯一看到的 按钮“确定”和“”取消(并创建新文件夹按钮) ShowNewFolderButton 属性设置为 true)。当我尝试完全相同的时候 使用不同形式的代码一切正常。

有什么想法吗?

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();

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.

Any ideas??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2024-09-05 20:31:10

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

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

否则您可以执行此操作(来自 FolderBrowserDialog 类)。

/// <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();
}

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());
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文