日期:2013-09-20  浏览次数:20511 次

using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.IO;

class FolderDialog:Form
{
  TreeView FolderTree;
  ImageList FolderTreeIcons;
  TreeNode RootNode;
  string[] IconFileNames={"MyComp.ico","Drive.ico","OpenFold.ico","ClsdFold.ico"};
  public FolderDialog()
  {
    //Manipulate ImageList
    FolderTreeIcons=new ImageList();
    for(int i=0;i<IconFileNames.Length;i++)
    {
      FolderTreeIcons.Images.Add(Image.FromFile(@"Images\" + IconFileNames[i]));
    }
    
    //Setting Properties for TreeView
    FolderTree=new TreeView();
    FolderTree.AfterSelect+=new TreeViewEventHandler(Node_Select);
    FolderTree.ImageList=FolderTreeIcons;
    FolderTree.Anchor=AnchorStyles.Top|AnchorStyles.Bottom|AnchorStyles.Left|AnchorStyles.Right;
        

    //Root Node
    RootNode=new TreeNode();
    RootNode.Text="My Computer";
    RootNode.Tag="My Computer";
    RootNode.ImageIndex=0;
    FolderTree.Nodes.Add(RootNode);
    FolderTree.SelectedNode=RootNode;
    FolderTree.SelectedNode.ImageIndex=0;

    this.Text="Browse For Folder";
    this.Size=new Size(300,300);
    FolderTree.Size=new Size(290,275);
  
    this.Controls.Add(FolderTree);
  }

  private void EnumDrives(TreeNode ParentNode)
  {
    if(ParentNode.Nodes.Count==0)
    {    
      foreach(string drive in Directory.GetLogicalDrives())
      {
         FolderTree.SelectedNode=ParentNode;
         TreeNode TempNode=new TreeNode();
         TempNode.Text=drive.Substring(0,drive.Length-1);
         TempNode.Tag=drive;
         TempNode.ImageIndex=1;
         TempNode.SelectedImageIndex=1;
         FolderTree.SelectedNode.Nodes.Add(TempNode);
         FolderTree.SelectedNode.Nodes[FolderTree.SelectedNode.Nodes.Count-1].EnsureVisible();
      }
    }
  }

  private void EnumDirectories(TreeNode ParentNode)
  {
    FolderTree.SelectedNode=ParentNode;
    string DirectoryPath=ParentNode.Tag.ToString();
    if(ParentNode.Nodes.Count==0)
    {
      if(DirectoryPath.Substring(DirectoryPath.Length-1)!=@"\")
        DirectoryPath+=@"\";
      try
      {
        foreach(string directory in Directory.GetDirectorie