Hari B

Hari B

  • NA
  • 139
  • 67.9k

TreeView Control logic Issue

Sep 19 2014 9:33 AM
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:
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