A better FolderBrowserDialog in C#

A better FolderBrowserDialog in C#

This code lets you get a list of folders in a tree view given a directory/subdirectory to start from.

In this example the code has been called asking for all directries in “c:\ATI”

Copy the code below the line and paste into a newly created form in Visual Studio.

// Set a folder view at any subdirectory eg e:\Windows
//
// Create a new form in your project - call it FolderViewRoot
// edit the FolderViewRoot.cs and paste the contents of this file in.
//
// to use on your program use the following code
//
// - FolderViewRoot secondForm = new FolderViewRoot();
// - secondForm.FVrootdirectory = "c:\ATI";
// - secondForm.FVrootdirectorylable = "Select Media Directory";
// - secondForm.Show();
// -
// - // returns string in FVrootdirectoryfull
// - MessageBox.Show(secondForm.FVrootdirectoryfull);
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

// change namespace to match yours
namespace audio_xml
{
    public partial class FolderViewRoot : Form
    {
        private System.Windows.Forms.TreeView FVdirectoryTreeView;
        private System.Windows.Forms.TextBox FVdirectoryRoot;
        private System.Windows.Forms.Label FVrootDirectoryLabel;
        private System.Windows.Forms.Button btnOk;
        private System.Windows.Forms.Button btnCancel;
        public String FVrootdirectoryfull = "";
        public String FVrootdirectory { get; set; }
        public String FVrootdirectorylable { get; set; }

        public FolderViewRoot()
        {
            InitializeComponent();
            FVInitializeComponent();
        }

        public void FVRootDirectory(String s)
        {
            FVrootdirectory = s;
        }

        public void FVRootDirectoryLable(String s)
        {
            FVrootdirectorylable = s;
        }

        private void FVFolderViewRoot_Load(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            CenterToScreen();
            FormBorderStyle = FormBorderStyle.FixedDialog;
            FVdirectoryTreeView.Nodes.Clear();
            String path = FVrootdirectory;
            FVdirectoryRoot.Text = FVrootdirectory;
            FVdirectoryTreeView.Nodes.Add(path);
            FVPopulateTreeView(path, FVdirectoryTreeView.Nodes[0]);
            Cursor.Current = Cursors.Default;
            FVrootDirectoryLabel.Text = FVrootdirectorylable;
        }

        private void FVPopulateTreeView(String directoryValue, TreeNode parentNode)
        {
            String[] directoryArray = Directory.GetDirectories(directoryValue);
            String substringDirectory;
            if (directoryArray.Length != 0)
            {
                foreach (String directory in directoryArray)
                {
                    substringDirectory = directory.Substring(directory.LastIndexOf('\\') + 1, directory.Length - directory.LastIndexOf('\\') - 1);
                    TreeNode myNode = new TreeNode(substringDirectory);
                    try
                    {
                        parentNode.Nodes.Add(myNode);
                        FVPopulateTreeView(directory, myNode);
                    }
                    catch (UnauthorizedAccessException) {}
                }
            }
        }

        private void FVbtnCancel_Click(object sender, EventArgs e)
        {
            FVrootdirectoryfull = "";
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void FVbtnOk_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(FVdirectoryTreeView.SelectedNode.FullPath);
            FVrootdirectoryfull = FVdirectoryTreeView.SelectedNode.FullPath;
            //MessageBox.Show(Fvrootdirectoryfull);
            DialogResult = DialogResult.OK;
            Close();
        }

        private void FVdirectoryTreeView_Click(object sender, EventArgs e)
        {
            btnOk.Enabled = true;
        }

        private void FVInitializeComponent()
        {
            FVdirectoryTreeView = new System.Windows.Forms.TreeView();
            FVdirectoryRoot = new System.Windows.Forms.TextBox();
            FVrootDirectoryLabel = new System.Windows.Forms.Label();
            btnOk = new System.Windows.Forms.Button();
            btnCancel = new System.Windows.Forms.Button();
            SuspendLayout();
            //
            // btnOK
            //
            btnOk.BackColor = Color.LightGray;
            btnOk.Text = "OK";
            btnOk.Location = new System.Drawing.Point(10, 310);
            btnOk.Size = new System.Drawing.Size(60, 30);
            btnOk.Click += new System.EventHandler(FVbtnOk_Click);
            btnOk.Enabled = false;
            //
            // btnCancel
            //
            btnCancel.BackColor = Color.LightGray;
            btnCancel.Text = "Cancel";
            btnCancel.Location = new System.Drawing.Point(235, 310);
            btnCancel.Size = new System.Drawing.Size(60, 30);
            btnCancel.Click += new System.EventHandler(FVbtnCancel_Click);
            //
            // FVrootDirectoryLabel
            //
            FVrootDirectoryLabel.Location = new System.Drawing.Point(10, 5);
            FVrootDirectoryLabel.Name = "FVrootDirectoryLabel";
            FVrootDirectoryLabel.Size = new System.Drawing.Size(284, 20);
            FVrootDirectoryLabel.Text = "Please Wait - Directory being read";
            //
            // directoryTreeRoot
            //
            FVdirectoryRoot.Location = new System.Drawing.Point(10, 25);
            FVdirectoryRoot.Name = "FVdirectoryRoot";
            FVdirectoryRoot.Size = new System.Drawing.Size(284, 20);
            FVdirectoryRoot.ReadOnly = true;
            //
            // directoryTreeView
            //
            FVdirectoryTreeView.Location = new System.Drawing.Point(10, 55);
            FVdirectoryTreeView.Name = "FVdirectoryTreeView";
            FVdirectoryTreeView.Size = new System.Drawing.Size(285, 245);
            FVdirectoryTreeView.TabIndex = 0;
            FVdirectoryTreeView.Click += new System.EventHandler(FVdirectoryTreeView_Click);
            //
            // TreeViewDirectoryStructureForm
            //
            AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            ClientSize = new System.Drawing.Size(305, 350);
            Controls.Add(FVdirectoryTreeView);
            Controls.Add(FVdirectoryRoot);
            Controls.Add(FVrootDirectoryLabel);
            Controls.Add(btnOk);
            Controls.Add(btnCancel);
            Name = "TreeViewDirectoryStructureForm";
            Text = "TreeViewDirectoryStructure";
            ResumeLayout(false);
            PerformLayout();
        }
    }
}