Treeview in .Net Core Console Application

Prabs 1 Reputation point
2021-03-31T09:00:03.517+00:00

Hi,
How to create treeview in .Net Core console application dynamically

Developer technologies .NET .NET Runtime
Developer technologies C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-04-01T08:19:19.347+00:00

    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.

    1 person found this answer helpful.

  2. Ken Tucker 5,861 Reputation points
    2021-03-31T09:53:57.95+00:00

    You cannot use any controls in a console application. If you need a GUI create a windows forms or WPF application


  3. Viorel 122.5K Reputation points
    2021-04-01T08:55:17.367+00:00

    According to https://www.bing.com/search?q=add+reference+windows+forms+console+core and https://learn.microsoft.com/en-US/dotnet/core/project-sdk/msbuild-props-desktop, try right-clicking the project in Solution Explorer, then select “Edit Project File” and add two elements:

    <Project Sdk="Microsoft.NET.Sdk">  
      <PropertyGroup>  
        . . .  
        <UseWindowsForms>true</UseWindowsForms>  
        <DisableWinExeOutputInference>true</DisableWinExeOutputInference>  
      </PropertyGroup>  
    </Project>  
    

    Now the next line should work:

    var t = new System.Windows.Forms.TreeView( );  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.