How to find duplicate files with name and FilePath from Computer using C#

PRA
 
on Sep 21, 2016 05:21 AM
2780 Views

Hi! It's Define duplicate file name with address worked. But now I want program search my computer and define duplicate file without choose file by opendialog.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 21, 2016 05:29 AM

Hi PRA,

Refer the below code. From the below code you will get all the files from your system as a list. Now you need to apply your logic to find duplicate files using for loop.

string file = string.Empty;
List<string> files = new List<string>();
string[] directoryForSearch = new string[] { "C", "D", "E" };
for (int k = 0; k < directoryForSearch.Length; k++)
{
    DirectoryInfo drive = new DirectoryInfo(directoryForSearch[k] + ":\\");
    DirectoryInfo[] folders = drive.GetDirectories("*.*");
    for (int i = 0; i < folders.Length; i++)
    {
        FileInfo[] allFiles = folders[i].GetFiles("*.*");
        if (allFiles.Length > 0)
        {
            for (int j = 0; j < allFiles.Length; j++)
            {
                files.Add(allFiles[j].DirectoryName + "\\" + allFiles[j].ToString());
            }
        }
    }
}