As you can see, the plus sign (+) is displayed in front of the item and the files on the disk are read. The Code is as follows:
Main program code:
[Csharp]
<Span style = "font-size: 14px;"> 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 FileManager
{
Public partial class Main: Form
{
Public Main ()
{
InitializeComponent ();
}
/// <Summary>
/// Load the Logical Disk File
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void Main_Load (object sender, EventArgs e)
{
DriveInfo [] myDrivers = DriveInfo. GetDrives ();
Foreach (DriveInfo di in myDrivers)
{
If (di. IsReady)
{
TreeNode tNode = new TreeNode (di. Name. Split (':') [0]);
TNode. Name = di. Name;
TNode. Tag = tNode. Name;
<Span style = "color: # ff0000;"> tNode </span>. <span style = "color: # ff0000;"> Nodes </span>. <span style = "color: # ff0000;"> Add </span> <span style = "color: # ff0000; "> (" </span> <span style = "color: # ff0000;"> DUMMY </span> <span style = "color: # ff0000;"> "); </span>
TvRoot. Nodes. Add (tNode );
}
}
// Add the right-click event
This. tvRoot. ContextMenuStrip = new TreeViewContextMenu (). Load ();
}
/// <Summary>
/// One of the two events that the TreeView must process
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void tvRoot_AfterSelect (object sender, TreeViewEventArgs e)
{
E. Node. Expand ();
}
/// <Summary>
/// One of the two events that the TreeView must process
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void tvRoot_BeforeExpand (object sender, TreeViewCancelEventArgs e)
{
TreeViewItems. Add (sender, e );
}
}
} </Span>
Code for adding a TreeNode to a TreeView:
[Csharp]
<Span style = "font-size: 14px;"> public static class TreeViewItems
{
Public static void Add (object sender, TreeViewCancelEventArgs e)
{
E. Node. Nodes. Clear ();
TreeNode tNode = e. Node;
// MessageBox. Show (tNode. Name );
String path = tNode. Name;
String [] dics = Directory. GetDirectories (path );
Foreach (string dic in dics)
{
TreeNode subNode = new TreeNode (new DirectoryInfo (dic). Name );
SubNode. Name = new DirectoryInfo (dic). FullName;
SubNode. Tag = subNode. Name;
<Span style = "color: # ff0000;"> subNode </span>. <span style = "color: # ff0000;"> Nodes </span>. <span style = "color: # ff6666;"> Add </span> <span style = "color: # ff0000;"> (""); </span>
TNode. Nodes. Add (subNode );
}
String [] files = Directory. GetFiles (path );
Foreach (string aFile in files)
{
TreeNode subNode = new TreeNode (new FileInfo (aFile). Name );
SubNode. Tag = aFile;
SubNode. Name = subNode. Text;
TNode. Nodes. Add (subNode );
}
}
} </Span>
Pay attention to e. Node. Nodes. Clear (); and add an empty Node to the Node:
TNode. Nodes. Add ("DUMMY ");
And
SubNode. Nodes. Add ("");
These are all ways to add empty nodes to display the "plus sign "... Delete the file when it is officially loaded.