Treeview bind folders and files

Source: Internet
Author: User

# 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.