Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As stated in the questions. I want to read files from multiple directories and display all of it in a single datagridview. I've found one that having the same problem but that one is using List<>.AddRange to get the directories while I'm using string[]. Please help. Here is my code snippet to fetch the directory and files :

C#
s1 = Directory.GetFiles(@"C:\Documents and Settings\Administrator\Desktop\FILE\7", "*.*");


and this is for reading it and filling it in a datagridview :

C#
for (int i = 0; i <= s1.Length - 1; i++)
        {

            try
            {
                if (i == 0)
                {
                    dt.Columns.Add("File_Name");
                    dt.Columns.Add("File_Type");
                    dt.Columns.Add("File_Size");
                    dt.Columns.Add("Create_Date");
                }

                FileInfo info = new FileInfo(s1[i]);
                FileSystemInfo sysInfo = new FileInfo(s1[i]);
                dr = dt.NewRow();

                dr["File_Name"] = sysInfo.Name;
                dr["File_Type"] = sysInfo.Extension;
                dr["File_Size"] = (info.Length / 1024).ToString();
                dr["Create_Date"] = sysInfo.CreationTime.Date.ToString("dd/MM/yyyy");
                dt.Rows.Add(dr);


                if ((info.Length / 1024) > 1500000)
                {
                    MessageBox.Show("" + sysInfo.Name + " had reach its size limit.");
                }



                if (dt.Rows.Count > 0)
                {
                    dataGridView1.DataSource = dt;
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show("Error : " + ex.Message);
                continue;
            }
        }

   }
Posted
Comments
Reza Ahmadi 5-Apr-12 4:43am    
It is not very clear. Do you mean you need to get files from a directory and it's sub directories and then display in a GridView?
nizam15 5-Apr-12 4:47am    
Sorry about that. I want to get the files from a directory and also sub directories but different root. Meaning, for example I got these 2 path :

1.C:\Documents and Settings\Administrator\Desktop\FILE
2.F:\gdimaging\data

so I want to read all the files within these two path and display it in a datagridview.

1 solution

If I understand what you are looking for, this may help, and it saves having to go back and get FileInfo;

C#
var y = (new DirectoryInfo(@"C:\")).GetFiles().ToList();
y.AddRange((new DirectoryInfo(@"C:\boot")).GetFiles());
 
Share this answer
 
Comments
nizam15 5-Apr-12 20:27pm    
Thanks Clifford!!my 5'ed!!:D

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