How to create a Tree View of the Windows Folder and extract all the file-folder info.

A treeview control in winforms can be used to emulate a extended windows explorer behaviour to show the file and folder attributes. Read on to find out how.

I have removed most of the designer code.  

Brief description of the control's usefulness:
Show All Dir - Shows the Drives in the system.  Corresponds to LoadTreeViewDir function
Create/Append - Appends info to the exisiting file. Corresponds to btnCreate_Click function.


The rest is the TreeView Control event code.



/// <summary>

/// Shell Class.
/// </summary>
public class Shell : System.Windows.Forms.Form
{
    //I have removed the designer code. following is just an excerpt. hope you can recreate the same.

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
         this.btnDisplay = new System.Windows.Forms.Button();
         this.mainMenu1 = new System.Windows.Forms.MainMenu();
         this.menuFile = new System.Windows.Forms.MenuItem();
         this.menuRfrsh = new System.Windows.Forms.MenuItem();
         this.menuExit = new System.Windows.Forms.MenuItem();
         this.menuHelp = new System.Windows.Forms.MenuItem();
         this.trvwDir = new System.Windows.Forms.TreeView();
         this.lblParent = new System.Windows.Forms.Label();
        this.lblRoot = new System.Windows.Forms.Label();
        this.lblLastWriteTime = new System.Windows.Forms.Label();
        this.lblLastAccessTime = new System.Windows.Forms.Label();
        this.lblCreationTime = new System.Windows.Forms.Label();
        this.lblAttributes = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.label4 = new System.Windows.Forms.Label();
        this.label5 = new System.Windows.Forms.Label();
        this.label6 = new System.Windows.Forms.Label();
        this.label7 = new System.Windows.Forms.Label();
        this.label8 = new System.Windows.Forms.Label();
        this.label9 = new System.Windows.Forms.Label();
        this.btnCreate = new System.Windows.Forms.Button();
         this.txtName = new System.Windows.Forms.TextBox();
         this.txtFullName = new System.Windows.Forms.TextBox();
         this.txtExtension = new System.Windows.Forms.TextBox();
         this.richTextBox1 = new System.Windows.Forms.RichTextBox();
         this.label10 = new System.Windows.Forms.Label();
        this.label11 = new System.Windows.Forms.Label();
    }

     /// <summary>
    /// Dummy string.
    /// </summary>
    private const string Dummy = "Dummy";

    /// <summary>
    /// ItemType enum.
    /// </summary>
    private enum ItemType
    {
         /// <summary>
        ///
        /// </summary>
        Directory = 1,
         /// <summary>
        ///
        /// </summary>
        File = 2
     }

    /// <summary>
    /// Handles the Click event of the menuRfrsh control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void mnuRefresh_Click(object sender, System.EventArgs e)
    {
         LoadTreeViewDir();
    }

    /// <summary>
    /// Handles the AfterSelect event of the trvwDir control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
    private void trvwDir_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {
        try
        {
             if (e.Node.Tag is ItemType)
             {
                 switch ((ItemType)e.Node.Tag)
                 {
                      case ItemType.File:
                        {
                            lblParent.Text = string.Empty;
                            lblRoot.Text = string.Empty;
                            lblParent.Enabled = false;
                            lblRoot.Enabled = false;
                            FileInfo fi = new FileInfo(e.Node.FullPath);
                              DisplayFileProp(fi);
                              break;
                         }
                     case ItemType.Directory:
                        {
                            lblParent.Enabled = true;
                            lblRoot.Enabled = true;
                            DirectoryInfo Di = new DirectoryInfo(e.Node.FullPath);
                            lblParent.Text = Di.Parent.Name;
                            lblRoot.Text = Di.Root.Name;
                              DisplayFileProp(Di);
                              break;
                         }
                     default:
                         {
                              ClearProperties();
                              break;
                         }
                 }
             }
             else
            {
                 ClearProperties();
             }
         }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, this.Text);
        }
     }



    /// <summary>
    /// Clears the properties.
    /// </summary>
    private void ClearProperties()
    {
        lblAttributes.Text = "";
        lblCreationTime.Text = string.Empty;
        txtExtension.Text = string.Empty;
        txtFullName.Text = string.Empty;
        lblLastAccessTime.Text = string.Empty;
        lblLastWriteTime.Text = string.Empty;
        txtName.Text = string.Empty;
        lblParent.Text = string.Empty;
        lblRoot.Text = string.Empty;
    }

    /// <summary>
    /// Displays the file prop.
    /// </summary>
    /// <param name="fsi">The fsi.</param>
    private void DisplayFileProp(FileSystemInfo fsi)
    {
        txtName.Text = fsi.Name;
        txtFullName.Text = fsi.FullName;
        lblAttributes.Text = fsi.Attributes.ToString();
        lblCreationTime.Text = fsi.CreationTime.ToString();
        lblLastAccessTime.Text = fsi.LastAccessTime.ToString();
        lblLastWriteTime.Text = fsi.LastWriteTime.ToString();
        txtExtension.Text = fsi.Extension;
    }

     /// <summary>
    /// Loads the tree view dir.
    /// </summary>
    private void LoadTreeViewDir()
    {
         trvwDir.Nodes.Clear();
         foreach (string strDrive in Directory.GetLogicalDrives())
        {
            TreeNode nod = trvwDir.Nodes.Add(strDrive);
             nod.Nodes.Add(Dummy);
         }
    }


    /// <summary>
    /// Handles the BeforeExpand event of the trvwDir control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param>
    private void trvwDir_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
     {
        try
        {
             e.Node.Nodes.Clear();
            AddFolders(e.Node);
            AddFiles(e.Node);
        }
         catch (IOException)
        {
             // Drive isnt ready...simply disregard the error.
        }
         catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), this.Text);
        }
     }

    /// <summary>
    /// Adds the files.
    /// </summary>
    /// <param name="nod">The nod.</param>
    private void AddFiles(TreeNode nod)
    {
         string strPath = nod.FullPath;
        foreach (string strFile in Directory.GetFiles(strPath))
        {
            TreeNode NewNode = nod.Nodes.Add(Path.GetFileName(strFile));
            NewNode.Tag = ItemType.File;
        }
    }

    /// <summary>
    /// Adds the folders.
    /// </summary>
    /// <param name="nod">The nod.</param>
    private void AddFolders(TreeNode nod)
    {
         string strPath = nod.FullPath;
        foreach (string strDir in Directory.GetDirectories(strPath))
        {
            TreeNode NewNode = nod.Nodes.Add(Path.GetFileName(strDir));
            NewNode.Tag = ItemType.Directory;
             NewNode.Nodes.Add(Dummy);
         }
    }

     /// <summary>
    /// Handles the Click event of the btnDisplay control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void btnDisplay_Click(object sender, System.EventArgs e)
    {
         LoadTreeViewDir();
    }

    /// <summary>
    /// Handles the Click event of the menuExit control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void menuExit_Click(object sender, System.EventArgs e)
    {
         this.Close();
    }


     /// <summary>
    /// Handles the Click event of the btnCreate control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    private void btnCreate_Click(object sender, System.EventArgs e)
    {
         int bufsize = 1024;
        string strName = txtName.Text;
        string strFullName = txtFullName.Text;
         string strPath = strFullName + "\\" + strName + txtExtension.Text;
        if (!(Directory.Exists(strFullName)))
            Directory.CreateDirectory(strFullName);
        if (!(File.Exists(strPath)))
        {
            FileStream fs = File.Create(strPath, bufsize);
            fs.Close();
        }
        StreamWriter sw = File.AppendText(strPath);
         sw.WriteLine(richTextBox1.Text);
         sw.Close();

         MessageBox.Show("
File successfully created");
    }

}

Kudos
By [)ia6l0 iii   Popularity  (2451 Views)