home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Tree View and List View / DirectoriesAndFiles / DirectoryTreeView.cs < prev    next >
Encoding:
Text File  |  2001-01-15  |  2.4 KB  |  88 lines

  1. //---------------------------------------------
  2. // DirectoryTreeView.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Windows.Forms;
  8.  
  9. class DirectoryTreeView: TreeView
  10. {
  11.      public DirectoryTreeView()
  12.      {
  13.                // Make a little more room for long directory names.
  14.  
  15.           Width *= 2;
  16.  
  17.                // Get images for tree.
  18.  
  19.           ImageList = new ImageList();
  20.           ImageList.Images.Add(new Bitmap(GetType(), "35FLOPPY.BMP"));
  21.           ImageList.Images.Add(new Bitmap(GetType(), "CLSDFOLD.BMP"));
  22.           ImageList.Images.Add(new Bitmap(GetType(), "OPENFOLD.BMP"));
  23.  
  24.                // Construct tree.
  25.  
  26.           RefreshTree();
  27.      }
  28.      public void RefreshTree()
  29.      {
  30.                // Turn off visual updating and clear tree.
  31.  
  32.           BeginUpdate();
  33.           Nodes.Clear();
  34.  
  35.                // Make disk drives the root nodes. 
  36.  
  37.           string[] astrDrives = Directory.GetLogicalDrives();
  38.  
  39.           foreach (string str in astrDrives)
  40.           {
  41.                TreeNode tnDrive = new TreeNode(str, 0, 0);
  42.                Nodes.Add(tnDrive);
  43.                AddDirectories(tnDrive);
  44.  
  45.                if (str == "C:\\")
  46.                     SelectedNode = tnDrive;
  47.           }
  48.           EndUpdate();
  49.      }
  50.      void AddDirectories(TreeNode tn)
  51.      {
  52.           tn.Nodes.Clear();
  53.  
  54.           string          strPath = tn.FullPath;
  55.           DirectoryInfo   dirinfo = new DirectoryInfo(strPath);
  56.           DirectoryInfo[] adirinfo;
  57.  
  58.           try
  59.           {
  60.                adirinfo = dirinfo.GetDirectories();
  61.           }
  62.           catch
  63.           {
  64.                return;
  65.           }
  66.  
  67.           foreach (DirectoryInfo di in adirinfo)
  68.           {
  69.                TreeNode tnDir = new TreeNode(di.Name, 1, 2);
  70.                tn.Nodes.Add(tnDir);
  71.  
  72.                // We could now fill up the whole tree with this statement:
  73.                //        AddDirectories(tnDir);
  74.                // But it would be too slow. Try it!
  75.           }
  76.      }
  77.      protected override void OnBeforeExpand(TreeViewCancelEventArgs tvcea)
  78.      {
  79.           base.OnBeforeExpand(tvcea);
  80.  
  81.           BeginUpdate();
  82.  
  83.           foreach (TreeNode tn in tvcea.Node.Nodes)
  84.                AddDirectories(tn);
  85.  
  86.           EndUpdate();
  87.      }
  88. }