Selecting a folder using a dialog box
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Selecting a folder using a dialog box

  1. #1
    Join Date
    Sep 2011
    Posts
    8

    Selecting a folder using a dialog box

    Hi

    I am trying to open a dialog box so that I would be able to select a folder so that I will return the filenames in that folder. I was trying to do this using the openfiledialog but my problem is that when I am on the folder I want to select, I can only select open to get to the files. Is there a similar dialog where it allows me to select a folder to take its path rather than open it. The following is the code i have used so far:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                string[] filepaths;
                string path;
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    path = Path.GetDirectoryName(openFileDialog1.FileName);
                    filepaths = Directory.GetFiles(path);
                    textBox1.Text = path;
                }
            }
    Thank you in advance.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,236

    Re: Selecting a folder using a dialog box

    See FolderBrowserDialog class in msdn.

  3. #3
    Join Date
    Sep 2011
    Posts
    8

    Re: Selecting a folder using a dialog box

    Hi

    Thank you very much for your reply. I managed to solve my problem using your suggestion. By any chance is there a way to make the FolderBrowseDialog look like the OpenDialog?

    Once again thank you.

    Regards

  4. #4
    Join Date
    Apr 2013
    Posts
    1

    Re: Selecting a folder using a dialog box

    some Sample

    FolderBrowseDialog fbd = new folderBrowseDialog();
    string path = null;
    if(DialogResult.OK == fbd.ShowDialog())
    {
    // Select directory
    path = fbd.SelectedPath;

    // get files
    if (Directory.Exists(fbd.SelectedPath))
    {
    foreach (string filesName in Directory.GetFiles(fbd.SelectedPath))
    {
    // Todo something with avaliable files in select directory


    }
    }


    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center