Doron Muzar
Doron Muzar

Reputation: 443

How do i make a nested FileInfo List?

What i want to do is to get all the directories and each directory the files inside and add it to a List. The List should look like this for example:

In index[0] {[fileinfo list and all the files here]}

So in each index I will have the directory and inside all the files of this directory.

I tried to do:

private void getfiles()
{
    List<List<FileInfo>> fileList = new List<List<FileInfo>>();
    for (int i = 0; i < BackgroundWorkerConfiguration.urlsDirectories.Count; i++)
    {
        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(BackgroundWorkerConfiguration.urlsDirectories[i]);
        fileList.AddRange(di.GetFiles("*.*", System.IO.SearchOption.AllDirectories).Where(x => x.Length > 0));
    }
    file_array = fileList.OrderBy(x => x.CreationTime).Select(x => x.FullName).ToArray();
    //file_array = fileList.OrderByDescending(x => x.CreationTime).Select(x => x.FullName).ToArray();
    timer1.Enabled = true;
}

I changed the List<FileInfo> to List<List<FileInfo>>, so a List in a List.

So in the end if i have 5 directories on the hard disk the List will have 5 indexs and in each index there will be a List with the files of the directory.

EDIT**

This is the timer1 tick event:

private void timer1_Tick(object sender, EventArgs e)
{
    try
    {     
        //this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        for (int i = 0; i < file_array.Length; i++)
        {

        }
        if (leave == true)
        {
            pb.Load(file_array[file_indxs]);
        }
        else
        {
            pbs[0].Load(file_array[file_indxs]);
        }
        file_indxs = file_indxs + 1;
        if (file_indxs >= file_array.Length)
        {
            file_indxs = 0;
        }
    }
    catch
    {
        timer1.Enabled = false;
    }
}

I have 8 pictureBoxes I create them in the form1 constructor. In this case in the timer tick event I assign the images to pbs[0] its the first pictureBox.

What I need to do is to use the nested list and assign each directory with the files inside to a pictureBox. So the first index in the nested list the files there should be assign to the first pictureBox pbs[0] then index 1 to pbs[1] and so on.

So if the nested list have 5 indexes it should be assigned to 5 pictureBoxes.

Instead assigning all the images to pbs[0] that's why I did the nested list.

Upvotes: 0

Views: 810

Answers (1)

King King
King King

Reputation: 63317

You need to group all the files by directory. There are some ways to do so, for example each time you prepare to enumerate files of a directory, just create a new List<FileInfo>, enumerate files and add them to the new list, each newly created list will be added to the root List<List<FileInfo>> (which can be converted to an array if you want). However I would like to use this approach which will modify your code as less as possible:

private void getfiles() {
  List<FileInfo> fileList = new List<FileInfo>();
  for (int i = 0; i < BackgroundWorkerConfiguration.urlsDirectories.Count; i++) {
    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(BackgroundWorkerConfiguration.urlsDirectories[i]);
    fileList.AddRange(di.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
                        .Where(x => x.Length > 0));
  }
  file_array = fileList.OrderBy(x => x.CreationTime)
                       .GroupBy(x => x.DirectoryName)
                       .Select(g => g.Select(x => x.FullName).ToList())
                       .ToArray();
  timer1.Enabled = true;
}

NOTE: The file_array will have type List<string>[] (an array of List<string>). Your problem looks like still unclear to me with the involvement of timer1, but this should solve your question (not your whole problem). You should ask another question for related issue. This answer just answers to the question you gave.

Upvotes: 1

Related Questions