John

John

  • NA
  • 43
  • 26k

Searching and opening files in a listBox

Jun 16 2013 6:48 PM

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 every time I attain a new book.


So far my snippets is as follow:

  1. // Button to display all java books
  2. private void button2_Click(object sender, EventArgs e)
  3. {
  4. listBox1.Items.Clear();
  5. string[] book = Directory.GetFiles(@"C:\Books\JAVA", "*.*", SearchOption.AllDirectories);
  6. foreach (string b in book)
  7. {
  8. string filePath = Path.GetFullPath(b);
  9. string entry = Path.GetFileName(b);
  10. listBox1.Items.Add(entry);
  11. }
  12. }
  13. // Button to open file
  14. private void button9_Click(object sender, EventArgs e)
  15. {
  16. string file = listBox1.SelectedItem.ToString();
  17. System.Diagnostics.Process.Start(file);
  18. }


So currently I can get the books in that particular folder to show, however I can not open the file unless I am displaying the path name which I want to avoid if possible.

If anyone has any suggestion, would appreciate it very much.


Cheers Guys



Answers (1)