如何从随机选择的多个文件中执行文件?

时间:2013-09-22 18:55:03

标签: c# windows process

我对这件事情很陌生。实际上这是我的第一份工作。我想要一个从文本框中读取随机文件数的程序。它有一个按钮来随机选择所选路径中的文件。我需要打开列表框中的文件。

我的问题是,当我双击列表框时,无论我查看哪个文件,它都会打开列表中的最后一个文件。我尝试添加在下面放两个斜杠的行。但它也没有用。我该怎么办?

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);

    }

1 个答案:

答案 0 :(得分:0)

您传入了randomchoose,此时已修复,请尝试此操作:

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
   if(listBox1.SelectedItem != null)
      Process.Start(((FileInfo)listBox1.SelectedItem).FullName);
}