Hi, I just finished making a program which displays all the files in a directory and lets me open them when selected. What I find is that I get every file found in the directory however not divided by sub-directory, is there a way i can do this showing it on the listbox?

Heres my code so far.

List<FileInfo> myBooks = new List<FileInfo>();

void fileInfoDelegate()
        {
            myBooks.Sort(
                delegate(FileInfo a, FileInfo b)
                {
                    return a.Name.CompareTo(b.Name);
                });
        }

//VideoTraining
        private void button8_Click(object sender, EventArgs e)
        {
            clear(); // clear lstbox b4 repopulate.
            myBooks.AddRange(new DirectoryInfo(@"C:VideoTraining\").GetFiles("*.*", SearchOption.AllDirectories));
            fileInfoDelegate();
            listBox1.DataSource = myBooks;
            boxempty();  // returns msg if lstbox is empty   
        }

        //OpenFile
    private void button13_Click(object sender, EventArgs e)
    {

        FileInfo TempInfo = myBooks[listBox1.SelectedIndex];
        System.Diagnostics.Process.Start(TempInfo.FullName);
    }

Thanks Guys.

You might want to consider a TreeView which is made for that sort of thing. If I'm not mistaken the only way to do it in the listbox is to use spaces or VBTab to offset a line. Doing it with a listbox means you have to define your own structure to hold the data or try and build the data on the fly which will require some sort of recursive function.

Wasnt too fond on using a Treeview, So I kept to the listbox. I used a somewhat recursive method to do it. It's not great, but it puts the files in a much better order which I can tell where it belongs to.

heres the code:

            clear();
            Stack<string> stack = new Stack<string>();

            string dir;
            stack.Push(@"VideoTraining");
            while (stack.Count > 0)
            {
                // Pop a directory
                dir = stack.Pop(); 
                string[] files = Directory.GetFiles(dir, "*.WMV");
                string[] files2 = Directory.GetFiles(dir, "*.MOV");

                foreach (string file in files)
                {
                    FileInfo info = new FileInfo(file);
                    listBox1.Items.Add(Path.GetDirectoryName(file).TrimEnd
                        (Path.DirectorySeparatorChar) + Path.GetFileName(file));
                   myBooks.Add(info);
                }
                foreach (string file in files2)
                {
                    FileInfo info = new FileInfo(file);
                    listBox1.Items.Add(Path.GetDirectoryName(file) + Path.GetFileName(file));
                    myBooks.Add(info);
                }
                string[] direct = Directory.GetDirectories(dir);
                Array.Reverse(direct);
                foreach (string directory in direct)
                {
                    // Push each directory into stack    
                    stack.Push(directory);
                }


        }
        boxempty();

Cheers.

I still should reconsider the tip of tinstaafl. TreeView is not so hard to learn as you think Click Here

I looked into it, but was having a bit of trouble getting the file(s) to open, especially from subfolders.

The code I got for that was:

    string dir = @"C:\Test\VideoTraining\";
        private void button1_Click(object sender, EventArgs e) // populate treeview
        {
            ListDirectory(treeView1, dir);            
        }

        private void ListDirectory(TreeView treeview1, string dir)
        {
            treeView1.Nodes.Clear();
            var rootDirectoryInfo = new DirectoryInfo(dir);
            treeView1.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
        }

       private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
        {
            var directoryNode = new TreeNode(directoryInfo.Name);
            foreach (var directory in directoryInfo.GetDirectories())
                directoryNode.Nodes.Add(CreateDirectoryNode(directory));
            foreach (var file in directoryInfo.GetFiles())
                directoryNode.Nodes.Add(new TreeNode(file.Name));
            return directoryNode;
        }

        private void button2_Click(object sender, EventArgs e) // to open file
        {
            string name = treeView1.SelectedNode.ToString().Replace("TreeNode: ",string.Empty);
            MessageBox.Show(dir + "\\" +  name);
            System.Diagnostics.Process.Start(dir + "\\" +  name);
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.