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()
                delegate(FileInfo a, FileInfo b)
                    return a.Name.CompareTo(b.Name);

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

    private void button13_Click(object sender, EventArgs e)

        FileInfo TempInfo = myBooks[listBox1.SelectedIndex];

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:

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

            string dir;
            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);
                        (Path.DirectorySeparatorChar) + Path.GetFileName(file));
                foreach (string file in files2)
                    FileInfo info = new FileInfo(file);
                    listBox1.Items.Add(Path.GetDirectoryName(file) + Path.GetFileName(file));
                string[] direct = Directory.GetDirectories(dir);
                foreach (string directory in direct)
                    // Push each directory into stack    



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)
            var rootDirectoryInfo = new DirectoryInfo(dir);

       private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
            var directoryNode = new TreeNode(directoryInfo.Name);
            foreach (var directory in directoryInfo.GetDirectories())
            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);