Click here to Skip to main content
15,881,763 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello ,
i have a foreach loop for DirectoryInfo in which i am getting 2 directory .

so in this loop i want to assign all the files in both directory into single FileInfo.
fileInfo[] FILE =null;
foreach(Directoryinfo ...)
{
FILE = dirinfo.getfiles();
}

but at the end i am not able to store files from both the dir.
the files from 2nd Dir is replacing the files of 1st Dir in FILE.

please help me.
Posted
Updated 1-Jun-18 6:06am

hi,
you need to use AddRange rather than '=', like this:

XML
List<FileInfo> FILE = new List<FileInfo>();
            DirectoryInfo[] Ddd;

            foreach (DirectoryInfo dirInfo in Ddd)
                FILE.AddRange(dirInfo.GetFiles());


Use a List to enable AddRange. In addition, I'v presumed that you will fill Ddd with you DirectoryInfos.


Cheers
 
Share this answer
 
v2
Comments
rohit24c 12-Jun-12 9:03am    
thanks buddy it works :)
Reza Ahmadi 12-Jun-12 10:51am    
Your welcome!
Help yourself with temporary list:
C#
FileInfo[] fileInfosArray;
List<fileinfo> tempList = new List<fileinfo>();

foreach(DirectoryInfo di in yourDirInfoCollection)
{
    tempList.AddRange(di.GetFiles());
}

fileInfosArray = tempList.ToArray();


Btw, this line of your code was the problem:
C#
FILE = dirinfo.getfiles();

It means SET FILE to become array of FileInfos from current directory,
while (as I can see from your question) you wanted to
ADD array of FileInfos from current directory to FILE.
 
Share this answer
 
v2
Okay, if I am understanding this correctly, you have 2 directories (folders) and you want the file info from both directories to list in one document. So, this program will list all the files in one directory, then list all the files in the second directory all in one csv or txt file. -
Here is a very simple (basic) solution for what you are trying to do:


using System;
using System.IO;

namespace ConsoleApplication1 
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo A = new DirectoryInfo("c:/Bank/TestFolderA");
            FileInfo[] a = A.GetFiles("*.*", SearchOption.AllDirectories);

            DirectoryInfo B = new DirectoryInfo("c:/Bank/TestFolderB");
            FileInfo[] b = B.GetFiles("*.*", SearchOption.AllDirectories);

            using (System.IO.StreamWriter file = new StreamWriter(@"c:\Bank\TestFolderC\TestResults1.csv"))
            {
                foreach (FileInfo x in a)
                {
                    Console.WriteLine(@"Copying {0}, {1}, {2}, {3} ", x.DirectoryName, x.Name, x.Extension, x.Length);
                    file.WriteLine("{0}, {1}, {2}, {3} ", x.DirectoryName, x.Name, x.Extension, x.Length);
                }

                foreach (FileInfo y in b)
                {
                    Console.WriteLine(@"Copying {0}, {1}, {2}, {3}", y.DirectoryName, y.Name, y.Extension, y.Length);
                    file.WriteLine("{0}, {1}, {2}, {3}", y.DirectoryName, y.Name, y.Extension, y.Length );
                }
            }
        }
    }
}

I Hope this helps with your situation and maybe help someone else in the future.
 
Share this answer
 
Comments
Richard MacCutchan 1-Jun-18 12:44pm    
Answered SIX years ago.

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