Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
string[] fileEntries = Directory.GetFiles(tbDirectory.Text);

                foreach (string filename in fileEntries)
                {
                    Icon ico = Icon.ExtractAssociatedIcon(filename.ToString());
                    smallImageList.Images.Add(ico);
                    //long fileSize = new System.IO.FileInfo(filename).Length;

                    for (int i = 0; i < fileEntries.Length; i++)
                    {
                        //lvi.SubItems.Add(GetSizeReadable(fileSize));
                        lvSums.Items.Add(filename.ToString().ToLower(), i);
                    }
                }
                foreach (ColumnHeader columns in lvSums.Columns)
                    columns.Width = -2;


I am trying to add to the listview the respected icon with its filename, no going as planned.
Posted
Comments
Mathi Mani 22-May-15 17:38pm    
Because you are having a FOR loop inside FOREACH.
Sergey Alexandrovich Kryukov 22-May-15 17:44pm    
Right. Would you elaborate it a bit, and post a formal answer, just to get rid of this trash? :-)
—SA

1 solution

I found it:
Took out the for loop and just added type int indexx and incremented for each file added. Works perfect.

C#
try
           {
               string[] fileEntries = Directory.GetFiles(tbDirectory.Text);

               foreach (string filename in fileEntries)
               {
                   Icon ico = Icon.ExtractAssociatedIcon(filename.ToString());
                   smallImageList.Images.Add(ico);
                   //long fileSize = new System.IO.FileInfo(filename).Length;

                   //for (int i = 0; i < fileEntries.Length; i++)
                   //{
                       //lvi.SubItems.Add(GetSizeReadable(fileSize));
                       lvSums.Items.Add(filename.ToString().ToLower(), index);
                       index++;
                   //}
               }
               foreach (ColumnHeader columns in lvSums.Columns)
                   columns.Width = -2;
           }
           catch { Exception ex;}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900