Mohamed Moumni

Mohamed Moumni

  • NA
  • 48
  • 7.8k

how to open a file selected in listbox ?

Jun 3 2017 7:22 PM
I have all the dirctories (2014, 2012), the files of each selected folder (.pdf) in the listbox 2
 
 
 
get the dirctories by this code , 
  1. if (FBD.ShowDialog() == DialogResult.OK)
  2. {
  3. listBox1.Items.Clear();
  4. DirectoryInfo[] diri_info =newDirectoryInfo(FBD.SelectedPath).GetDirectories();
  5. foreach (DirectoryInfo diri in diri_info)
  6. {
  7. listBox1.Items.Add(diri);
  8. }
and i get the files by this code
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. if (listBox1.SelectedIndex >= 0)
  4. {
  5. DirectoryInfo dirictory_choisis = (DirectoryInfo)listBox1.SelectedItem;
  6. FileInfo[] files = dirictory_choisis.GetFiles();
  7. listBox2.Items.Clear();
  8. foreach (FileInfo file in files)
  9. {
  10. listBox2.Items.Add(file);
  11. }
  12. }
  13. else
  14. {
  15. MessageBox.Show("selectioner un dossier");
  16. }
  17. }
Now how I can open the selected file (.pdf)  ?
  
i use this code but dosn't work ( throw an exception file dosn't found) 
  1. private void listBox2_Click(object sender, EventArgs e)  
  2.       {  
  3.           
  4.               FileInfo file =(FileInfo) listBox2.SelectedItem;  
  5.               Process.Start(file.Name);  
  6.             
  7.       }  
 

Answers (1)