官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 天全县| 河曲县| 宁远县| 玉林市| 本溪| 抚松县| 元江| 潢川县| 永善县| 皮山县| 巴林左旗| 扶绥县| 西宁市| 新昌县| 理塘县| 台山市| 横山县| 搜索| 定安县| 安新县| 临海市| 翁牛特旗| 鄄城县| 汤阴县| 名山县| 江都市| 玛纳斯县| 盐亭县| 南部县| 土默特左旗| 唐河县| 桃园县| 安塞县| 宝坻区| 洪湖市| 新田县| 栖霞市| 丰城市| 星子县| 丰城市| 湖南省|