Populating the TreeView control

We can see the implementation of the AllowedExtensions property when we look at the PopulateBookList() method. All that this method does is populate the TreeView control with files and folders found at the selected source location. Consider the following code:

public void PopulateBookList(string paramDir, TreeNode paramNode) 
{ 
    DirectoryInfo dir = new DirectoryInfo(paramDir); 
    foreach (DirectoryInfo dirInfo in dir.GetDirectories()) 
    { 
        TreeNode node = new TreeNode(dirInfo.Name); 
        node.ImageIndex = 4; 
        node.SelectedImageIndex = 5; 
 
        if (paramNode != null) 
            paramNode.Nodes.Add(node); 
        else 
            tvFoundBooks.Nodes.Add(node); 
        PopulateBookList(dirInfo.FullName, node); 
    } 
    foreach (FileInfo fleInfo in dir.GetFiles().Where
(x => AllowedExtensions.Contains(x.Extension)).ToList()) { TreeNode node = new TreeNode(fleInfo.Name); node.Tag = fleInfo.FullName; int iconIndex = Enum.Parse(typeof(Extention),
fleInfo.Extension.TrimStart('.'), true).GetHashCode(); node.ImageIndex = iconIndex; node.SelectedImageIndex = iconIndex; if (paramNode != null) paramNode.Nodes.Add(node); else tvFoundBooks.Nodes.Add(node); } }

The first place we need to call this method is obviously from within itself, as this is a recursive method. The second place we need to call it is from the btnSelectSourceFolder button click event:

private void btnSelectSourceFolder_Click(object sender, EventArgs e) 
{ 
    try 
    { 
        FolderBrowserDialog fbd = new FolderBrowserDialog(); 
        fbd.Description = "Select the location of your eBooks and 
documents"; DialogResult dlgResult = fbd.ShowDialog(); if (dlgResult == DialogResult.OK) { tvFoundBooks.Nodes.Clear(); tvFoundBooks.ImageList = tvImages; string path = fbd.SelectedPath; DirectoryInfo di = new DirectoryInfo(path); TreeNode root = new TreeNode(di.Name); root.ImageIndex = 4; root.SelectedImageIndex = 5; tvFoundBooks.Nodes.Add(root); PopulateBookList(di.FullName, root); tvFoundBooks.Sort(); root.Expand(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }

This is all quite straightforward code. Select the folder to recurse and populate the TreeView control with all the files found that match the file extension contained in our AllowedExtensions property.

We also need to look at the code when someone selects a book in the tvFoundBooks TreeView control. When a book is selected, we need to read the properties of the selected file and return those properties to the file details section:

private void tvFoundBooks_AfterSelect(object sender, TreeViewEventArgs e) 
{ 
    DocumentEngine engine = new DocumentEngine(); 
    string path = e.Node.Tag?.ToString() ?? ""; 
 
    if (File.Exists(path)) 
    { 
        var (dateCreated, dateLastAccessed, fileName, 
fileExtention, fileLength, hasError) =
engine.GetFileProperties(e.Node.Tag.ToString()); if (!hasError) { txtFileName.Text = fileName; txtExtension.Text = fileExtention; dtCreated.Value = dateCreated; dtLastAccessed.Value = dateLastAccessed; txtFilePath.Text = e.Node.Tag.ToString(); txtFileSize.Text = $"{Round(fileLength.ToMegabytes(),
2).ToString()} MB"; } } }

You will notice that it is here that we are calling the method GetFileProperties() on the DocumentEngine class that returns the tuple.