Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing;
- using Microsoft.VisualBasic.FileIO;
- namespace FileExplorer_TreeView
- {
- /* Class :FileExplorer
- * Author : Chandana Subasinghe
- * Date : 10/03/2006
- * Discription : This class use to create the tree view and load
- * directories and files in to the tree
- *
- * Michael van Strien - 16 March 2010 * Slight modifications were made to change this into a standalone control
- */
- class FileExplorer : TreeView
- {
- public FileExplorer()
- {
- this.BeforeExpand += customBeforeExpand;
- CreateTree(this);
- }
- /* Method :CreateTree
- * Author : Chandana Subasinghe
- * Date : 10/03/2006
- * Discription : This is use to creat and build the tree
- *
- */
- public bool CreateTree(TreeView treeView)
- {
- bool returnValue = false;
- try {
- // Create Desktop
- TreeNode desktop = new TreeNode();
- desktop.Text = "Desktop";
- desktop.Tag = "Desktop";
- desktop.Nodes.Add("");
- treeView.Nodes.Add(desktop);
- // Get driveInfo
- foreach (DriveInfo drv in DriveInfo.GetDrives())
- {
- TreeNode fChild = new TreeNode();
- if (drv.DriveType == DriveType.CDRom)
- {
- fChild.ImageIndex = 1;
- fChild.SelectedImageIndex = 1;
- }
- else if (drv.DriveType == DriveType.Fixed)
- {
- fChild.ImageIndex = 0;
- fChild.SelectedImageIndex = 0;
- }
- fChild.Text = drv.Name;
- fChild.Nodes.Add("");
- treeView.Nodes.Add(fChild);
- returnValue = true;
- }
- }
- catch (Exception ex)
- {
- returnValue = false;
- }
- return returnValue;
- }
- /* Method :EnumerateDirectory
- * Author : Chandana Subasinghe
- * Date : 10/03/2006
- * Discription : This is use to Enumerate directories and files
- *
- */
- public TreeNode EnumerateDirectory(TreeNode parentNode)
- {
- try {
- DirectoryInfo rootDir;
- // To fill Desktop
- Char [] arr={'\\'};
- string [] nameList=parentNode.FullPath.Split(arr);
- string path = "";
- if (nameList.GetValue(0).ToString() == "Desktop")
- {
- path = SpecialDirectories.Desktop+"\\";
- for (int i = 1; i < nameList.Length; i++)
- {
- path = path + nameList[i] + "\\";
- }
- rootDir = new DirectoryInfo(path);
- }
- // for other Directories
- else {
- rootDir = new DirectoryInfo(parentNode.FullPath + "\\");
- }
- parentNode.Nodes[0].Remove();
- foreach (DirectoryInfo dir in rootDir.GetDirectories())
- {
- TreeNode node = new TreeNode();
- node.Text = dir.Name;
- node.Nodes.Add("");
- parentNode.Nodes.Add(node);
- }
- //Fill files
- foreach (FileInfo file in rootDir.GetFiles())
- {
- TreeNode node = new TreeNode();
- node.Text = file.Name;
- node.ImageIndex = 2;
- node.SelectedImageIndex = 2;
- parentNode.Nodes.Add(node);
- }
- }
- catch (Exception ex)
- {
- //TODO :
- }
- return parentNode;
- }
- private void customBeforeExpand(object sender, TreeViewCancelEventArgs e)
- {
- if (e.Node.Nodes[0].Text == "")
- {
- TreeNode node = this.EnumerateDirectory(e.Node);
- }
- //TreeView::BeforeExpand();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement