I am relatively new to C# but I have experience in Java.
I am making an applicationt that takes an input from the user as a path (like C:\my pics\) and checks this path for duplicate files.
I have started to try to find a solution and I have come up with one solution. I get the list of files and add it to a String[] just the path to the files. Then I add that to one of my list boxes so the user can select one picture at a time. When the user selects a picture my program goes through the list and checks for duplicates. If it finds some then it displays those copies in another list that the user sees and can select.
The problem is that this means that the program needs to do a heavy processing work everytime the user selects a picture from the first list. I would like it to do all the work in the beginning before the user starts manipulating the files.
My current code is like this:
private void buttonStart_Click(object sender, RoutedEventArgs e)
{
clearStatusBox();
updateStatusBox("Started..");
//bwstart.RunWorkerAsync();
String path = textBoxPath.Text;
try
{
FileInfo fileinfo = new FileInfo(path);
}
catch (Exception ea)
{
updateStatusBox("Illigal path.");
return;
}
allfiles = getALLFilesWithExtension(path, "jpg");
listBoxOriginals.ItemsSource = allfiles;
updateStatusBox("Finished.");
}
/*
* Gets all the files in the current directory and subdirectories matching the extension.
* Returns an attay with the files.
*/
private String[] getALLFilesWithExtension(String path, String extension)
{
string[] filePaths = Directory.GetFiles(path, "*."+extension,SearchOption.AllDirectories);
return filePaths;
}
private void listBoxOriginals_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ArrayList dup = new ArrayList();
dup = checkForDuplicates(listBoxOriginals.SelectedItem.ToString());
listBoxDuplicates.ItemsSource = dup;
}
/*
* This method takes a file name string and checks for duplicates.
*/
private ArrayList checkForDuplicates(String path)
{
FileInfo file = new FileInfo(path);
int index = 0;
int dupindex = 0;
ArrayList duplicates = new ArrayList();
foreach(string str in allfiles)
{
if (checkIfDuplicate(file,new FileInfo(str)))
{
duplicates.Add(str);
dupindex++;
}
index++;
}
/* NOT USED
while (index < allfiles.Length)
{
if (checkIfDuplicate(file,new FileInfo(allfiles[index])))
{
}
index++;
}
*/ NOT USED
return duplicates;
}
/*
* This method takes two fileinfos and checks if they are duplicates or not.
* Returns true if the two fileinfos are duplicates. A duplicate file of info1 is a file
* that has the same size in bytes and the same write time.
* Returns true if info1 is the same size, has the same lastwritedate and is not the same file as info2.
* Returns false if info1 or info2 ToString() equals "BLANK".
*/
private Boolean checkIfDuplicate(FileInfo info1, FileInfo info2)
{
if (info2.ToString().Equals("BLANK") || info1.ToString().Equals("BLANK"))
{
return false;
}
if (info1.Length == info2.Length && info1.LastWriteTime.Equals(info2.LastWriteTime) && !info1.ToString().Equals(info2.ToString()))
{
return true;
}
else
{
return false;
}
}
I also made another solution but I don't even want to post that one because it took the computer forever to go through the whole list.
Has someone made something like this before or can someone think of a good solution?
Thank you


Sign In
Create Account

Back to top









