bala3569
bala3569

Reputation: 11010

Compare two Folder Contents using c#

I need to compare two folder contents based on

1.No of files
2.Size of files
3 date

and i got an error index out of bound exception in this piece of code

 private void SeekFiles(string Root)
    {
    string[] Files = System.IO.Directory.GetFiles(Root);
    string[] Folders = System.IO.Directory.GetDirectories(Root);
    FileInfo File;
       for(int i=0; i< Folders.Length; i++)
    {
    File = new FileInfo(Files[i]);
    FolderSize += File.Length;
    }

    for(int i=0; i< Folders.Length-1; i++)
    {
    SeekFiles(Folders[i]);
    }
    } 

Any Suggestion??

Upvotes: 1

Views: 2801

Answers (3)

Andrei Pana
Andrei Pana

Reputation: 4502

You are using Files[i] but i < Folders.Length in the first for.

Upvotes: 2

decyclone
decyclone

Reputation: 30830

Looks like you are using wrong index on a wrong collection :

for(int i=0; i< Folders.Length; i++)
{
    File = new FileInfo(Files[i]);
    FolderSize += File.Length;
}

Should be :

for(int i=0; i< **Files.Length**; i++)
{
    File = new FileInfo(Files[i]);
    FolderSize += File.Length;
}

Upvotes: 3

The Archetypal Paul
The Archetypal Paul

Reputation: 41749

for(int i=0; i< Folders.Length; i++)

{
File = new FileInfo(Files[i]);
FolderSize += File.Length;
}

This should be Files.Length

Upvotes: 1

Related Questions