CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2001
    Posts
    8

    BrowseForFolder (c#)

    Does anyone know how to get the "Browse For Folder" dialog to show up using C#, and give me back the path upon dismissal. I tried adding a reference to Shell32.dll and using the "ShellClass" class to call "BrowseForDialog(...)". This works to display the dialog, but it returns a Shell32.Folder object, and I have no idea how to get the path from that. If anyone has any ideas, I would sure appreciate some help. thanks


  2. #2
    Join Date
    Jun 2001
    Posts
    8

    Re: BrowseForFolder (c#)

    I figured it out. Here it is if you are curious...

    First add a reference to Shell32.dll. Next, wherever you want the browse dialog box to show, do this...


    some_func()
    {
    Shell32.ShellClass shell_class = new Shell32.ShellClass();

    //Note that param 3 and param 4 control the appearance
    //of the dialog box. These defaults are a good start
    Shell32.Folder3 folder_3 = (Shell32.Folder3) shell_class.BrowseForFolder(this.Handle.ToInt32(), "Browse for whatever reason", 0, 0);

    //This is just to prove it worked.
    MessageBox.Show(folder_3.Self.Path);
    }




    If there is a better way, I don't know it.


  3. #3
    Join Date
    Jul 2001
    Posts
    11

    Re: BrowseForFolder (c#)

    There is a better way. Use following code

    using System;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;


    namespace TestProject
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>

    public class OpenFolderDialog : FolderNameEditor
    {
    FolderBrowser fb;
    public OpenFolderDialog()
    {
    fb = new FolderBrowser();

    }
    public string GetFolder()
    {
    DialogResult dr = fb.ShowDialog();
    return fb.DirectoryPath;
    }
    }
    }

    calling code

    OpenFolderDialog open = new OpenFolderDialog();

    this.textBox1.Text = open.GetFolder();

    Hope this 'll solve your problems


  4. #4
    Join Date
    Jan 2003
    Posts
    2
    Here is an answer from Microsoft. I just tried it and it works:
    http://support.microsoft.com/default...en-us%3b306285

  5. #5
    Join Date
    Jan 2003
    Posts
    10
    Ok guys, I have seen so many slutions but still why doesn't anybody point out the fact that there is something called "OpenFileDialog" in the toolbar.. you just add that and then

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    data_string = openFileDialog1.FileName;
    }

    Or if you want more files just check the multi select option for the openFileDialog and go :

    int i;
    openFileDialog1.ShowDialog();
    for(i=0;i<openFileDialog1.FileNames.Length;i++)
    if(doesNotExist(openFileDialog1.FileNames[i])==1)
    fileList.Items.Add(openFileDialog1.FileNames[i]);

    ..if u use a list view control... or just adapt this to whatever suits your needs...

    Please take note that I'm somewhat of a begginer at programming and I might be quite off here.. sorry if I missunderstood the topic...

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    did you got the working around ?

    how did you solve it > plz let us know atleast. I was interested in ur problem.. solution

    thanx
    Paresh

  7. #7
    Join Date
    Jan 2003
    Location
    Sweden
    Posts
    115

    Re: BrowseForFolder (c#)

    Seems like no one ever posted the easy way of doing it:
    Code:
    FolderBrowserDialog fbd = new FolderBrowserDialog();
    if (fbd.ShowDialog() == DialogResult.OK)
    {
       string myPath = fbd.SelectedPath;
    }
    Maybe this has been added in more recent versions of .NET and was not available at the original date of this thread, but I still thought I should do this addendum just in case anyone else comes up with the same question and does a forum search.

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: BrowseForFolder (c#)

    Heh, but 4 years ago.. OP could have died or given up coding by now..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured