# Region treeview bind folders and files
/// <Summary>
/// Bind to the tree according to the folder
/// </Summary>
/// <Param name = "treeview"> </param>
/// <Param name = "FilePath"> </param>
/// <Returns> </returns>
Public bool SetTreeNoByFilePath (TreeView treeview, string FilePath, ImageList imgs)
{
Treeview. Nodes. Clear ();
Treeview. ImageList = imgs;
Try
{
Foreach (DirectoryInfo direc in new DirectoryInfo (FilePath). GetDirectories ())
{
TreeNode tn = new TreeNode (direc. Name );
Tn. Text = direc. FullName;
SetTreeNodeIco (tn, "dir", imgs );
Tn. Tag = direc. FullName;
SetSubDirectoryTreenode (direc, tn, imgs );
Treeview. Nodes. Add (tn );
}
Foreach (FileInfo finfo in new DirectoryInfo (FilePath). GetFiles ())
{
TreeNode temptreenode = new TreeNode (finfo. Name );
Temptreenode. Tag = finfo. FullName;
Temptreenode. Text = finfo. Name;
SetTreeNodeIco (temptreenode, finfo. Extension, imgs );
Treeview. Nodes. Add (temptreenode );
}
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Set the sub-directory
/// </Summary>
/// <Param name = "direc"> directory path </param>
/// <Param name = "tn"> </param>
/// <Param name = "imglist"> </param>
Private void SetSubDirectoryTreenode (DirectoryInfo direc, TreeNode tn, ImageList imglist)
{
Foreach (DirectoryInfo dir in new DirectoryInfo (direc. FullName). GetDirectories ())
{
TreeNode temptn = new TreeNode (dir. Name );
Temptn. Tag = dir. FullName;
SetTreeNodeIco (temptn, "dir", imglist );
Temptn. Text = dir. Name;
Tn. Nodes. Add (temptn );
Foreach (FileInfo fileinfo in new DirectoryInfo (dir. FullName). GetFiles ())
{
TreeNode temptreenode = new TreeNode (fileinfo. Name );
Temptreenode. Tag = fileinfo. FullName;
Temptreenode. Text = fileinfo. Name;
SetTreeNodeIco (temptreenode, fileinfo. Extension, imglist );
Temptn. Nodes. Add (temptreenode );
}
SetSubDirectoryTreenode (dir, temptn, imglist );
}
}
/// <Summary>
/// Set a small icon for the treeview
/// </Summary>
/// <Param name = "tn"> </param>
/// <Param name = "strExt"> </param>
/// <Param name = "imgs"> </param>
Private void SetTreeNodeIco (TreeNode tn, string strExt, ImageList imgs)
{
String ext = strExt. Replace (".","");
If (ext. ToLower () = "dir ")
{
Tn. ImageIndex = imgs. Images. IndexOfKey ("close ");
Tn. SelectedImageIndex = imgs. Images. IndexOfKey ("open ");
}
Else if (ext. ToLower () = "doc" | ext. ToLower () = "rar" | ext. ToLower () = "txt ")
{
Tn. ImageIndex = imgs. Images. IndexOfKey (ext );
Tn. SelectedImageIndex = imgs. Images. IndexOfKey (ext );
}
Else
{
Tn. ImageIndex = imgs. Images. IndexOfKey ("other ");
Tn. SelectedImageIndex = imgs. Images. IndexOfKey ("other ");
}
}
# Endregion
# Region bind only folders
/// <Summary>
/// Bind to the tree according to the folder
/// </Summary>
/// <Param name = "treeview"> </param>
/// <Param name = "FilePath"> </param>
/// <Returns> </returns>
Public bool SetTreeNoByFilePath (TreeView treeview, string FilePath)
{
Treeview. Nodes. Clear ();
Try
{
Foreach (DirectoryInfo direc in new DirectoryInfo (FilePath). GetDirectories ())
{
TreeNode tn = new TreeNode (direc. Name );
Tn. Text = direc. FullName;
Tn. Tag = direc. FullName;
SetSubDirectoryTreenode (direc, tn );
Treeview. Nodes. Add (tn );
}
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Set the sub-directory
/// </Summary>
/// <Param name = "direc"> directory path </param>
/// <Param name = "tn"> </param>
/// <Param name = "imglist"> </param>
Private void SetSubDirectoryTreenode (DirectoryInfo direc, TreeNode tn)
{
Foreach (DirectoryInfo dir in new DirectoryInfo (direc. FullName). GetDirectories ())
{
TreeNode temptn = new TreeNode (dir. Name );
Temptn. Tag = dir. FullName;
Temptn. Text = dir. Name;
Tn. Nodes. Add (temptn );
Foreach (FileInfo fileinfo in new DirectoryInfo (dir. FullName). GetFiles ())
{
TreeNode temptreenode = new TreeNode (fileinfo. Name );
Temptreenode. Tag = fileinfo. FullName;
Temptreenode. Text = fileinfo. Name;
Temptn. Nodes. Add (temptreenode );
}
SetSubDirectoryTreenode (dir, temptn );
}
}
# Endregion