using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TreeView
{
public partial class Form1 : Form
{
bool flag=false;
public Form1()
{
InitializeComponent();
this.treeView1.NodeMouseClick +=
new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
InitIconView();
GetDriver();
}
private void InitIconView()
{
Icon ico1 = new Icon("..//ico//APP.ico");
Icon ico2 = new Icon("..//ico//DRIVE.ico");
Icon ico3 = new Icon("..//ico//Floppy Disk.ico");
Icon ico4 = new Icon("..//ico//CDDRIVE.ico");
Icon ico5 = new Icon("..//ico//CLSDFOLD.ico");
Icon ico6 = new Icon("..//ico//OPENFOLD.ico");
imageList1.Images.Add(ico1);
imageList1.Images.Add(ico2);
imageList1.Images.Add(ico3);
imageList1.Images.Add(ico4);
imageList1.Images.Add(ico5);
imageList1.Images.Add(ico6);
}
private void GetDriver()
{
treeView1.Nodes.Clear();
treeView1.ImageList = imageList1;
TreeNode td = new TreeNode("我的电脑", 0, 0);
td.Tag = "_my_computer";
int[] driver_type = new int[Directory.GetLogicalDrives().Length];
treeView1.Nodes.Add(td);
string[] astrDrives = Directory.GetLogicalDrives();
DriveInfo[] allDrives = DriveInfo.GetDrives();
int iImageIndex=1;
int iSelectedIndex=1;
try
{
int cnt=0;
foreach (DriveInfo d in allDrives)
{
driver_type[cnt++]=(int)d.DriveType;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
for(int i=0;i<astrDrives.Length;i++)
{
switch (driver_type[i])
{
case 0://为分区的盘
case 1://DRIVE_NO_ROOT_DIR The root directory does not exist. 1
case 2://DRIVE_REMOVABLE移动磁盘 The disk can be removed from the drive. 2
case 3: iImageIndex = 1; iSelectedIndex = 1; break;
case 4://DRIVE_REMOTE The drive is a remote (network) drive. 4
case 5: iImageIndex = 3; iSelectedIndex = 3; break;//光驱
case 6://DRIVE_RAMDISK The drive is a RAM disk.
default: break;
}
/* if (str == "A://")
{ iImageIndex = 2; iSelectedIndex = 2; }
else if (str == "G://")
{ iImageIndex = 3; iSelectedIndex = 3; }
else
{ iImageIndex = 1; iSelectedIndex = 1; }*/
TreeNode tnDrive = new TreeNode(astrDrives[i], iImageIndex, iSelectedIndex);
tnDrive.Tag = astrDrives[i];
treeView1.Nodes[0].Nodes.Add(tnDrive);
DirectoryInfo dir=new DirectoryInfo(astrDrives[i]);
if (dir.Exists)
GetDirectories(dir.GetDirectories(), tnDrive);
if (astrDrives[i] == "C://")
{ treeView1.SelectedNode = tnDrive; }
treeView1.EndUpdate();
}
}
private void GetDirectories(DirectoryInfo[] subDirs,
TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs=null;
foreach (DirectoryInfo subDir in subDirs)
{
if ((subDir.Attributes & FileAttributes.Hidden) != 0)
{
continue;
}
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir.FullName;
try
{
subSubDirs = subDir.GetDirectories();
}
catch { };
if (subSubDirs!=null&&subSubDirs.Length != 0)
{
aNode.Nodes.Clear();
GetSecondDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
private void GetSecondDirectories(DirectoryInfo[] subDirs,
TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs = null;
foreach (DirectoryInfo subDir in subDirs)
{
if ((subDir.Attributes & FileAttributes.Hidden) != 0)
{
continue;
}
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir.FullName;
try
{
subSubDirs = subDir.GetDirectories();
}
catch { };
nodeToAddTo.Nodes.Add(aNode);
}
}
void treeView1_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
{
TreeNode tn=e.Node;
//DirectoryInfo dir=null;
// DirectoryInfo son=null;
if (tn.Tag.ToString() != treeView1.Nodes[0].Tag.ToString())
{
try
{
DirectoryInfo dir = new DirectoryInfo(tn.Tag.ToString());
tn.Nodes.Clear();
GetDirectories(dir.GetDirectories(), tn);
}
//try
//{
// foreach (TreeNode son in tn.Nodes)
// {
// }
//}
catch (System.Exception ex)
{
//MessageBox.Show(ex.Message);
}
//MessageBox.Show("1");
}
}
}
}