0

I have a program which gives random files. It is simple but I'm quite new to this. Im having trouble with creating fileinfo list of files. I added a contextmenustrip which has multiple choose of file genre (e.g: video files, text files..)

I wanted to define string array with cntxtmnustrp. and want it make new array and combine with previous. But it didnt work. Should I create a arraylist and add each list to this?

   public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    Random r = new Random();
    string path1;          
   DirectoryInfo dif;
  // List<FileInfo> files;
    FileInfo[] files;
    FileInfo[] newfiles;


    int randomchoose;
    int fok;
    int kok, pd;

    string[] filetypes;     



    private void button1_Click(object sender, EventArgs e)
    {

        FolderBrowserDialog hoho = new FolderBrowserDialog();  // yeni dosya yeri

        hoho.ShowNewFolderButton = true;

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

            foreach (string ft in filetypes)
        {
           files = dif.GetFiles("*.ft", SearchOption.AllDirectories);
          //files.AddRange(dif.GetFiles(string.Format("*.{0}", ft), SearchOption.AllDirectories));

            newfiles = newfiles.Concat(files);
        }                                                            

           //pd = liste.Length;

           pd = files.Length;
            kok = pd;
        }                  
    }

        }       
    private void button1_Click_1(object sender, EventArgs e)
    {
        listBox1.Sorted = true;

    }

    private void cesit_Click(object sender, EventArgs e)
    {
        //contextMenuStrip1.Show();
        contextMenuStrip1.Show(this.PointToScreen(cesit.Location));
    }


    private void videoFilesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        filetypes = new string[2] { "txt", "png" };

    }

    private void musicFilesToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //tur = ".png";
        //textBox4.Text = tur;
    }

    private void textFilesToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }

}
emmett
  • 193
  • 2
  • 14

1 Answers1

0

Assuming I understand what you mean, I'd make the files array into a List, by replacing:

FileInfo[] files;

with:

List<FileInfo> files;

That means you'd change:

files = dif.GetFiles("*.ft", SearchOption.AllDirectories);

to:

files.AddRange(dif.GetFiles(string.Format("*.{0}", ft) SearchOption.AllDirectories));

You can then get rid of the list concatenation:

newfiles = newfiles.Concat(files);
Arran
  • 24,648
  • 6
  • 68
  • 78
  • I tried but it gives error."file" was null. and also it cant get .Lenght from "file". where did I do wrong? – emmett Sep 25 '13 at 21:14
  • Thanks! I changed "List files" to "List files = new List()". and chjange "file.lenght" to "file.Count" It works fine know. But I got a question, willbe glad if you answer it. I replace c.m.strip with combobox. I want it gets new list when you change combobox. is it ok to just copy that "foreach..." into comboBox1_SelectedIndexChanged..or how can I get it back to listing files? – emmett Sep 26 '13 at 16:29