Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Oct 29th, 2010  |  syntax: C#  |  size: 2.84 KB  |  hits: 77  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using Microsoft.VisualBasic.FileIO;
  8.  
  9.  
  10. namespace FileExplorer_TreeView
  11. {
  12. /* Class :FileExplorer
  13. * Author : Chandana Subasinghe
  14. * Date : 10/03/2006
  15. * Discription : This class use to create the tree view and load
  16. * directories and files in to the tree
  17. *
  18. * Michael van Strien - 16 March 2010 * Slight modifications were made to change this into a standalone control
  19. */
  20. class FileExplorer : TreeView
  21. {
  22. public FileExplorer()
  23. {
  24. this.BeforeExpand += customBeforeExpand;
  25.  
  26. CreateTree(this);
  27. }
  28.  
  29. /* Method :CreateTree
  30. * Author : Chandana Subasinghe
  31. * Date : 10/03/2006
  32. * Discription : This is use to creat and build the tree
  33. *
  34. */
  35.  
  36. public bool CreateTree(TreeView treeView)
  37. {
  38. bool returnValue = false;
  39.  
  40. try {
  41. // Create Desktop
  42. TreeNode desktop = new TreeNode();
  43. desktop.Text = "Desktop";
  44. desktop.Tag = "Desktop";
  45. desktop.Nodes.Add("");
  46. treeView.Nodes.Add(desktop);
  47. // Get driveInfo
  48. foreach (DriveInfo drv in DriveInfo.GetDrives())
  49. {
  50.  
  51. TreeNode fChild = new TreeNode();
  52. if (drv.DriveType == DriveType.CDRom)
  53. {
  54. fChild.ImageIndex = 1;
  55. fChild.SelectedImageIndex = 1;
  56. }
  57. else if (drv.DriveType == DriveType.Fixed)
  58. {
  59. fChild.ImageIndex = 0;
  60. fChild.SelectedImageIndex = 0;
  61. }
  62. fChild.Text = drv.Name;
  63. fChild.Nodes.Add("");
  64. treeView.Nodes.Add(fChild);
  65. returnValue = true;
  66. }
  67.  
  68. }
  69. catch (Exception ex)
  70. {
  71. returnValue = false;
  72. }
  73. return returnValue;
  74.  
  75. }
  76.  
  77. /* Method :EnumerateDirectory
  78. * Author : Chandana Subasinghe
  79. * Date : 10/03/2006
  80. * Discription : This is use to Enumerate directories and files
  81. *
  82. */
  83. public TreeNode EnumerateDirectory(TreeNode parentNode)
  84. {
  85.  
  86. try {
  87. DirectoryInfo rootDir;
  88.  
  89. // To fill Desktop
  90. Char [] arr={'\\'};
  91. string [] nameList=parentNode.FullPath.Split(arr);
  92. string path = "";
  93.  
  94. if (nameList.GetValue(0).ToString() == "Desktop")
  95. {
  96. path = SpecialDirectories.Desktop+"\\";
  97.  
  98. for (int i = 1; i < nameList.Length; i++)
  99. {
  100. path = path + nameList[i] + "\\";
  101. }
  102.  
  103. rootDir = new DirectoryInfo(path);
  104. }
  105. // for other Directories
  106. else {
  107.  
  108. rootDir = new DirectoryInfo(parentNode.FullPath + "\\");
  109. }
  110.  
  111. parentNode.Nodes[0].Remove();
  112. foreach (DirectoryInfo dir in rootDir.GetDirectories())
  113. {
  114.  
  115. TreeNode node = new TreeNode();
  116. node.Text = dir.Name;
  117. node.Nodes.Add("");
  118. parentNode.Nodes.Add(node);
  119. }
  120. //Fill files
  121. foreach (FileInfo file in rootDir.GetFiles())
  122. {
  123. TreeNode node = new TreeNode();
  124. node.Text = file.Name;
  125. node.ImageIndex = 2;
  126. node.SelectedImageIndex = 2;
  127. parentNode.Nodes.Add(node);
  128. }
  129.  
  130.  
  131.  
  132. }
  133.  
  134. catch (Exception ex)
  135. {
  136. //TODO :
  137. }
  138.  
  139. return parentNode;
  140. }
  141.  
  142. private void customBeforeExpand(object sender, TreeViewCancelEventArgs e)
  143. {
  144. if (e.Node.Nodes[0].Text == "")
  145. {
  146. TreeNode node = this.EnumerateDirectory(e.Node);
  147. }
  148. //TreeView::BeforeExpand();
  149. }
  150. }
  151. }