Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / Windows Forms

Directory Comparer - A recursive tool to compare 2 folders

Rate me:
Please Sign up or sign in to vote.
4.71/5 (13 votes)
9 Jan 2012CPOL14 min read 93.2K   7.3K   28  
Directory Comparer is an extensible tool that could be used to compare 2 folders
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DirectoryComparer.Services
{
    public class DirectoryLister
    {
        public static List<string> GetAllFiles(string rootFolder, bool isRecursive)
        {
            if (!Directory.Exists(rootFolder))
            {
                throw new Exception("Root folder does not exist");
            }

            SearchOption option = isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
            string[] files = Directory.GetFiles(rootFolder, "*.*", option);

            return files.ToList();
        }

        public static List<string> GetAllFiles(string rootFolder)
        {
            if (!Directory.Exists(rootFolder))
            {
                throw new Exception("Root folder does not exist");
            }

            string[] filesAndFolders = Directory.GetFiles(rootFolder, "*.*", SearchOption.TopDirectoryOnly);
            filesAndFolders = filesAndFolders.Concat(Directory.GetDirectories(rootFolder)).ToArray();

            return filesAndFolders.ToList();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United States United States
Just another passionate software developer!

Some of the contributions to the open source world - a blog engine written in MVC 4 - sBlog.Net. Check it out here. For the codeproject article regarding sBlog.Net click here!

(Figuring out this section!)

Comments and Discussions