folderBrowserDialog不能在c#中工作

本文关键字:工作 不能 folderBrowserDialog | 更新日期: 2023-09-27 18:03:10

当我在c#中使用folderBrowserDialog点击按钮选择文件夹时对话框不显示,对话框结果设置为Cancel自动。下面是Button_Click后面的代码:

private void glassButton1_Click(object sender, EventArgs e)
{
    DialogResult result = folderBrowserDialog1.ShowDialog();//here Dialog is not shown and result=Cancel
    if (result==DialogResult.OK)
    {
        folderBrowserDialog1.ShowNewFolderButton = true;
        backupPath = folderBrowserDialog1.SelectedPath.ToString();
        if (Directory.Exists(backupPath))
            backupTextBox.Text = backupPath;
        //else MessageBox.Show("path is invalid", "error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
    }
}

我该如何修复它?谢谢。

folderBrowserDialog不能在c#中工作

添加STAThread属性到main方法

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        ...        
    }
}

这是代码,它对我来说很好。

using (var dialog = new FolderBrowserDialog())
    if (dialog.ShowDialog() == DialogResult.OK)
    {
         // some code...
    }

您的代码工作正常。结果为对话sult。OK当你点击FolderBrowserDialog "OK Button"时。当结果值设置为dialog sult时,单击"取消"或"关闭"按钮。取消

from project properties -> build section -> platform target,我选中了Prefer 32位复选框,它解决了我的问题