Hello Guys,
Am having a new assignment and a new problem. I should make C# application witch shows drive letters in combo box, directories and only .txt files in tree view, and I should be able to open text file from tree view in text box using Open button. I all ready have code to show me drives letters in combo box, but I don’t know how to get tree view from selected drive in tree view control.
Thanks all for helping me.

private void Form1_Load(object sender, EventArgs e)
        {
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            for (int i = 0; i <= drives.Length - 1; i++)
            {
                comboBox1.Items.Add(drives[i].Name);
                comboBox1.SelectedIndex = -1;
            }
        }

Edited 5 Years Ago by __avd: Added [code] tags.

Attachments assignment2.JPG 22.38 KB

Like I said.. This is assignment witch I have to solve. I must have it separated. It would be probably much easier to have all in one place.

I found the Answer. Maybe it can help to someone else.

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 Assignment___Tekst_čitač
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }


        private void Form1_Load(object sender, EventArgs e)
        {
           
            DriveInfo[] drives = DriveInfo.GetDrives();
            for (int i = 0; i <= drives.Length - 1; i++)
            {
                comboBox1.Items.Add(drives[i].Name);
                comboBox1.SelectedIndex = -1;
            }
        }
            

        

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            
            DirectoryInfo path = new DirectoryInfo(comboBox1.SelectedItem.ToString());
            
            
            try
            {
                foreach (DirectoryInfo dir in path.GetDirectories())
                {
                    TreeNode node = treeView1.Nodes.Add(dir.Name);
                    node.Nodes.Add(" ");
                }
                
                foreach (FileInfo file in path.GetFiles())
                {
                    if (file.Extension.ToLower() == ".txt")
                    {
                        TreeNode node = treeView1.Nodes.Add(file.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

       

        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            try
            {
               
                TreeNode courentNode = e.Node;
                DirectoryInfo folderPaht = new DirectoryInfo(comboBox1.SelectedItem + courentNode.FullPath);
                
                foreach (DirectoryInfo dir in folderPaht.GetDirectories())
                {
                    string fileName = dir.Name;
                    TreeNode node = courentNode.Nodes.Add(fileName);
                    node.Nodes.Add(" ");
                }
                
                foreach (FileInfo file in folderPaht.GetFiles())
                {
                    string ext = file.Extension;
                    if (ext.ToLower() == ".txt")
                    {
                        TreeNode newNode = courentNode.Nodes.Add(file.Name);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string fulPath = comboBox1.SelectedItem + treeView1.SelectedNode.FullPath;
            if (fulPath.ToLower().EndsWith(".txt"))
            {
                StreamReader read = new StreamReader(fulPath);
                richTextBox1.Text = read.ReadToEnd();
                read.Close();
            }
            else
                MessageBox.Show("");
        }
  
    }
}

In this BeforeExpand event.

private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            try
            {
               
                TreeNode courentNode = e.Node;
                DirectoryInfo folderPaht = new DirectoryInfo(comboBox1.SelectedItem + courentNode.FullPath);
                
                foreach (DirectoryInfo dir in folderPaht.GetDirectories())
                {
                    string fileName = dir.Name;
                    TreeNode node = courentNode.Nodes.Add(fileName);
                    node.Nodes.Add(" ");
                }
          
            }
          }

This code for assignment TekstČitač doesn't work for me. I tried, but nothing happens. Is it working for someone else?

This question has already been answered. Start a new discussion instead.