【问题标题】:Using FolderBrowserDialog folder as path使用 FolderBrowserDialog 文件夹作为路径
【发布时间】:2015-10-01 00:51:57
【问题描述】:

您好,我正在尝试让用户能够使用 FolderBrowserDialog 打开文件夹,然后将该文件夹设置为路径。我不太确定如何,这是我到目前为止的做法:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        InitializeGUI();
    }
    public void InitializeGUI()
    {

         int fileCount = Directory.GetFiles(?????, "*.xml", SearchOption.AllDirectories).Length;
        textBox1.Text = fileCount.ToString();
    }
    //When the user clicks select file
    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        DialogResult result = fbd.ShowDialog();
        string[] files = Directory.GetFiles(fbd.SelectedPath);
    }
}

应该把什么作为文件路径?我希望文件路径是选定的文件夹。

【问题讨论】:

  • 如果结果 == DialogResult.OK 那么 fbd.SelectedPath 将是选定的文件夹。如果对话框被取消,我不知道它是什么。我知道如果对话框被取消,你不应该尝试使用它。
  • 您是否尝试在您的InitializeGUI 方法中使用选定的路径?如果是这种情况,则必须在方法运行之前显示对话框。

标签: c#


【解决方案1】:

我使用以下内容来显示标准的 FolderBrowserDialog

var folderBrowserDialog = new FolderBrowserDialog();

folderBrowserDialog.SelectedPath = "c:\temp";
folderBrowserDialog.ShowNewFolderButton = true;
folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
folderBrowserDialog.Description = "Select the folder to generate the new documents within";

var dialogResult = folderBrowserDialog.ShowDialog();

然后可以使用

检索所选目录
var selectedDirectory = folderBrowserDialog.SelectedPath;

【讨论】:

    猜你喜欢
    • 2012-07-23
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2014-08-18
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多