Can anyone tell me why this code doesn't work?

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("");
        }
  
    }
}

Recommended Answers

All 4 Replies

What is not working exactly?

I checked the code, and repaired it:

private void button1_Click(object sender, EventArgs e)
        {
            string fulPath = comboBox1.SelectedItem + treeView1.SelectedNode.FullPath;
            if (fulPath.ToLower().EndsWith(".txt"))
            {
                using (StreamReader sr = new StreamReader(fulPath))
                {
                    richTextBox1.Clear();
                    string line;
                    while ((line = sr.ReadLine()) != null)
                        richTextBox1.AppendText(line);
                }
            }
            else
                MessageBox.Show("");
        }

Hope it helps, (sretno)
Mitja

Thanks Mitja for correction, but it still isn't working. I think the problem could be in the first part of code(for listing drives in comboBox), because when I try to run just that part, nothing happens.

Your code works fine for me, and even my code which I added.
Double check again.
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.