Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello all,

If i select one folder that displays all the sub-folders in Tree structured format. But, here i have one condition i.e If i select one folder that should display all the sub-folders which is having the name started with "_"(underscrore) in Tree structure format. If any folder name is not started with "_" (underscrore) that folder should not displayed in the Tree structure.

I have written the code like below:

C#
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows.Forms;
            using System.IO;
            namespace tree_browse
            {
            public partial class Form1 : Form
            {
                 string strSearchPath = string.Empty;
                 public Form1()
                 {
                     InitializeComponent();
                 }
                 private void button1_Click(object sender, EventArgs e)
                 {
                       FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                       folderBrowserDialog.SelectedPath = strSearchPath;
                       if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                       {
                           Properties.Settings.Default.Save();
                           strSearchPath = folderBrowserDialog.SelectedPath;
                           GetTree(strSearchPath);
                       }
                 }
                 void GetTree(string strSearchPath)
                 {
                      treeView1.Nodes.Clear();
                      SetNode(treeView1, strSearchPath);
                      treeView1.TopNode.Expand();
                  }
                 void SetNode(TreeView treeName, string path)
                  {
                       DirectoryInfo dirInfo = new DirectoryInfo(path);
                       TreeNode node = new TreeNode(dirInfo.Name);
                       node.Tag = dirInfo;
                       GetFolders(dirInfo, node);
                       GetFiles(dirInfo, node);
                        treeName.Nodes.Add(node);
                  }
                  void GetFolders(DirectoryInfo d, TreeNode node)
                  {
                     try
                       {
                          DirectoryInfo[] dInfo = d.GetDirectories();
                          if (dInfo.Length > 0)
                          {
                               TreeNode treeNode = new TreeNode();
                               foreach (DirectoryInfo driSub in dInfo)
                               {
                                   treeNode = node.Nodes.Add(driSub.Name, driSub.Name, 0, 0);
                                   GetFiles(driSub, treeNode);
                                   GetFolders(driSub, treeNode);
                               }
                           }
                       }
                      catch (Exception ex) { }
                  }
                void GetFiles(DirectoryInfo d, TreeNode node)
                {
                    //if you want to search .doc or docx file then:
                    // var files = d.GetFiles("*.doc*");
                    var files = d.GetFiles("*.*");
                    FileInfo[] subfileInfo = files.ToArray<FileInfo>();
                    if (subfileInfo.Length > 0)
                    {
                         for (int j = 0; j < subfileInfo.Length; j++)
                         {
                         //Checking for Hiddend files
                         bool isHidden = ((File.GetAttributes(subfileInfo[j].FullName) &      FileAttributes.Hidden) == FileAttributes.Hidden);
                              if (!isHidden)
                              {
                                   TreeNode treeNode = new TreeNode();
                                   string path = subfileInfo[j].FullName;
                                   string name = subfileInfo[j].Name;
                                   treeNode = node.Nodes.Add(path, name);
                               }
                          }
                    }
            }
      }
}

Please correct this code. Help me....

Thanks in Advance
Posted
Updated 19-Sep-14 4:30am
v3
Comments
[no name] 19-Sep-14 9:54am    
Well the obvious problem is that you are not checking to see if the directory starts with "-" anywhere in your code.
BillWoodruff 19-Sep-14 10:22am    
Please format this code using the CP editor, so it's easy to read.
Member 11030923 19-Sep-14 10:48am    
now code is Formate
Member 11030923 19-Sep-14 10:31am    
now code is Formate in CP
ZurdoDev 19-Sep-14 12:11pm    
Where are you stuck?

1 solution

The problem is solved by using some elementary practical logic. First of all, take all the file/directory names from the file system; it's not possible to create a file mask to add all file except the files matching some mask. So, take them all.

Then, concentrate your attention on the point where you add a new node to the tree. It would be the best to promote (abstract out) this piece of code in a separate method. Let's assume you do it. Apparently, you will always have a name of the file system object passed to this function as a string argument. Check this string for being "_*" (use string.StartsWith) and, it it matched, return from this function immediately. Yes, as simple as that.

Also, don't hard-code this '_' (and no other immediate constants, especially strings, not even ""; except, say, 0, 1 and null). This character should became a parameter of your algorithm.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900