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:
C#
// 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);
}
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
VulpesRank #55 · 29.2K reputationPosted Jun 16, 2013, 3:31 PM EDT
class BookHelper
{
public string FullPath { get; set; }
public string FileName { get; set; }
}
and now change the code in your button_Click handlers as follows:
private void button2_Click(object sender, EventArgs e)
{
string[] book = Directory.GetFiles(@"C:\Books\JAVA", "*.*", SearchOption.AllDirectories);
List
foreach (string b in book)
{
BookHelper bh = new BookHelper();
bh.FullPath = Path.GetFullPath(b);
bh.FileName = Path.GetFileName(b);
list.Add(bh);
}
listBox1.DataSource = list;
listBox1.DisplayMember = "FileName";
listBox1.ValueMember = "FullPath";
}
private void button9_Click(object sender, EventArgs e)
{
string file = listBox1.SelectedValue.ToString();
System.Diagnostics.Process.Start(file);
}