0

I'm quite new to this things. Actually it is my first work. I want a program that reads random file count from textbox. and it has a button to randomize files from selected path. I need to open files in the listbox.

My problem is when I double click on the listbox, it opens the last file in the list no matter what file I d.clicked. I tried to add lines that put two slash before below. But it also didnt work. What can I do?

public Form1()
    {
        InitializeComponent();
    }
    Random r = new Random();
    string path1;           
    DirectoryInfo dif;
    FileInfo[] files;             
   int randomchoose;
      //FileInfo[] files2; 
     //int hoho; 
    int[] randomcount;

    private void button1_Click(object sender, EventArgs e)
    {

        FolderBrowserDialog hoho = new FolderBrowserDialog();  
        hoho.ShowNewFolderButton = true;

        if (hoho.ShowDialog() == DialogResult.OK) 
        {
            path1 = hoho.SelectedPath; 
            textBox1.Text = path1;
            dif = new DirectoryInfo(path1);  
            files = dif.GetFiles();              
        }

    }

    private void btnrasgele_Click(object sender, EventArgs e)
    {           
        randomcount = new int[Convert.ToInt32(textBox3.Text)];
      // int hoho=0;
        foreach (int k in randomcount)
        {               
            int pd = files.Length;

            randomchoose = r.Next(0, Convert.ToInt32(pd + 1));
            listBox1.Items.Add(files[randomchoose]); 
       //files2[hoho] = files[randomchoose].FullName;           
            }
        }

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {

         //listBox1.SelectedIndex = hoho;
         //Process.Start(files2[hoho].FullName);
          Process.Start(files[randomchoose].FullName);

    }
korrupt
  • 200
  • 11

1 Answers1

0

You passed in the randomchoose which is fixed at that point, try this instead:

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
   if(listBox1.SelectedItem != null)
      Process.Start(((FileInfo)listBox1.SelectedItem).FullName);
}
King King
  • 61,710
  • 16
  • 105
  • 130
  • no, sadly it gives "win32exception was unhandled". I guess we need full path inside Process.Start(); – korrupt Sep 22 '13 at 19:27
  • @mehmtDemir It's my mistake, I thought you add `FullName` as each item of your listbox, but it's `FileInfo`, just updated. – King King Sep 22 '13 at 19:36
  • Thanks, it works. If I want to extend this to its subdirectories, what can I do? Appreciated. – korrupt Sep 22 '13 at 19:52
  • @mehmtDemir you may want to use a `ListView` to display all the `subdirectories` of the selected folder. It looks nearly the same to the `Windows Explorer folder listview`. You can also use a `TreeView` for this purpose. Just get the `DirectoryInfo` and use the `GetDirectories` method to get all the subdirectories and add them as the `child nodes` of the current node in `TreeView`... it's a little much work to do. You should try yourself first and if encountering any trouble, try posting another question. – King King Sep 22 '13 at 19:56
  • I replace with "files = dif.GetFiles("*", SearchOption.AllDirectories);" and It works now. is it that simple or is there a more efficient way to do this? Thanks again. – korrupt Sep 22 '13 at 22:35
  • @mehmtDemir if you just want to get all the files without caring about its `parent directories` (so that we can show the files in tree structure), that would be fine to keep using the `ListBox`. – King King Sep 23 '13 at 02:23
  • It is me again. It was working well until I added this http://stackoverflow.com/a/19137152/2763129 . (files.Add(new FileInfo(file));) Can you refix it please? – korrupt Oct 04 '13 at 20:51