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. treeview#nodemouseclick );
Initiconview ();
Getdriver ();
}
Private void initiconview ()
{
Icon ico1 = new icon (".. // ICO // app. ICO ");
Icon ico2 = new icon (".. // ICO // drive. ICO ");
Icon IO3 = 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 (IO3 );
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 ("My Computer", 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: // partitioned Disk
Case 1: // drive_no_root_dir the root directory does not exist. 1
Case 2: // drive_removable Mobile Disk 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; // Optical Drive
Case 6: // drive_ramdisk the drive is a RAM disk.
Default: break;
}
/* If (STR = "://")
{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 ");
}
}
}
}