The treeview object has no meaning in console applications. Do you want to get tree-structured data in console applications?
That doesn't need a treeview object, but a class like this:
class Node
{
public string Name { get; set; }
public List<Node> Children { get; set; }
public Node()
{
Children = new List<Node>();
}
}
You can use this class to store the tree structure and display it.
static Node Node = new Node();
static void Main(string[] args)
{
LoadDirectory(@"d:\test");
List<Node> nodes = Node.Children;
//......
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
public static void LoadDirectory(string Dir)
{
DirectoryInfo di = new DirectoryInfo(Dir);
Node.Name = di.Name;
LoadFiles(Dir, Node);
LoadSubDirectories(Dir, Node);
}
private static void LoadSubDirectories(string dir, Node node)
{
string[] subdirectoryEntries = Directory.GetDirectories(dir);
foreach (string subdirectory in subdirectoryEntries)
{
DirectoryInfo di = new DirectoryInfo(subdirectory);
Node node1 = new Node();
node1.Name = di.Name;
node.Children.Add(node1);
LoadFiles(subdirectory, node1);
LoadSubDirectories(subdirectory, node1);
}
}
private static void LoadFiles(string dir, Node node)
{
string[] Files = Directory.GetFiles(dir, "*.*");
foreach (string file in Files)
{
FileInfo fi = new FileInfo(file);
Node node1 = new Node();
node1.Name = fi.Name;
node.Children.Add(node1);
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.