Hi all,

Im want to create a program for myself which will search my directories for ebooks and let me select the one i want to read. So for instance my first form has a button for C# Books, I want that then to produce a list of books found on my computer and let me pick one to read.

I know a way i can do this using oledb and msaccess and having a link field in the table. However the purpose is I do not wish to do this everytime I attain a new book.

no code snippets as of yet as i've just started the program

If anyone knows a way or can point me in the right direction, would appreciate it very much.
Cheers Guys.

Recommended Answers

All 4 Replies

call Directory.GetFiles() as shown here. But be warned that this will take a long time to search your entire hard drive for the files you want. It would be a lot faster to use an MS Access or other SQL database to keep the links to the files.

If you keep all your ebooks in one folder you can minimize the search time of the GetFiles method.

If you use DirectoryInfo.GetFiles you can put the results into a List<FileInfo> and sorting is very easy with a delegate to compare names. Using that as the datasource for a listbox will give you a list of file names

from here you can use the SelectedIndexChanged event to trap your selection. Since the list in the listbox and in your List<FileInfo> are in the same order you can declare a temporary fileinfo object to equal the selected file in the list, using the selectedindex of the listbox. Now you can extract the full path of your selection.

If you need to make any changes do them to the list then update the listbox with the new list.

If my explanation is too complicated I can show you a snippet, it's much more simpler and cleaner than it sounds.

Thanks for the responses guys, just run into a bit of a problem. With a listBox I am finding that i could only get the file if im showing the directory. Is there a way i could do this with just displaying the book name.

Snippet so far:

// Button to display all java books
private void button2_Click(object sender, EventArgs e) 
        {
            listBox1.Items.Clear();
            string[] book = Directory.GetFiles(@"C:\Books\JAVA", "*.*", SearchOption.AllDirectories);
            foreach (string b in book)
            {
                string filePath = Path.GetFullPath(b);
                string entry = Path.GetFileName(b);
                listBox1.Items.Add(entry);  
            }
        }
// Button to open file
private void button9_Click(object sender, EventArgs e)
        {
            string file = listBox1.SelectedItem.ToString();
            System.Diagnostics.Process.Start(file);
        }

Here's some code that shows how to fill the listbox, then handle the button.click event to start running the file. This basically follows my instructions from my previous post.

        List<FileInfo> MyEBooks = new List<FileInfo>();
        private void button2_Click(object sender, EventArgs e)
        {    
            listBox1.Items.Clear();
            MyEBooks.AddRange(new DirectoryInfo(@"C:\Books\JAVA").GetFiles("*.*", SearchOption.AllDirectories));
            MyEBooks.Sort(
                delegate(FileInfo a,FileInfo b)
                {
                    return a.Name.CompareTo(b.Name);
                });    
            listBox1.DataSource = MyEBooks;
        }    
        private void button1_Click(object sender, EventArgs e)
        {
            FileInfo TempInfo = MyEBooks[listBox1.SelectedIndex];
            System.Diagnostics.Process.Start(TempInfo.FullName);
        }

With this code your files will be added to the listbox sorted, and will only show the file name not the whole path. That is stored in the list which has the fileinfo for every file in that folder. This way if you want different sorting, or filtering all the info you need is already there.

commented: sorted the problem +0
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.