I have to take the files from a directory one by one and look in another directory for a file with the same name. Once they match, I have to call mostrarResultadoComparacionPropiedades()
passing their paths as parameters. My code is this:
static void recorrerDirectoriosYCompararPorFicheros(string dir1, string dir2)
{
System.IO.DriveInfo dirOrigen = new System.IO.DriveInfo(dir1);
System.IO.DriveInfo dirDestino = new System.IO.DriveInfo(dir2);
System.IO.DirectoryInfo dirOrigenInfo = dirOrigen.RootDirectory;
System.IO.DirectoryInfo dirDestinoInfo = dirDestino.RootDirectory;
System.IO.FileInfo[] fileDirOrigenNames = dirOrigenInfo.GetFiles("*.*");
System.IO.FileInfo[] fileDirDestinoNames = dirDestinoInfo.GetFiles("*.*");
for (int i = 0;i<=(fileDirOrigenNames.Length)-1;i++ )
{
System.IO.FileInfo file = fileDirOrigenNames[i];
for (int j = 0; j <= (fileDirDestinoNames.Length)-1; j++)
{
System.IO.FileInfo file2 = fileDirDestinoNames[j];
if (file.Name.Equals(file2.Name))
{
mostrarResultadoComparacionPropiedades(file.DirectoryName, file2.DirectoryName);
}
}
}
}
Here are the methods to compare two files by properties:
static bool compareFilesByProperties(string file1, string file2)
{
DateTime fechaCreacion1 = File.GetCreationTime(file1);
DateTime ultimaModificacion1 = File.GetLastWriteTime(file1);
DateTime fechaCreacion2 = File.GetCreationTime(file2);
DateTime ultimaModificacion2 = File.GetLastWriteTime(file2);
if ((fechaCreacion1.Date.Equals(fechaCreacion2.Date)) &&(ultimaModificacion1.Date.Equals(ultimaModificacion2.Date)))
{
Console.WriteLine("Fecha de Creación y última modificación son iguales");
return true;
}
Console.WriteLine("Fecha de Creación o última modificación NO son iguales");
return false;
}
static void mostrarResultadoComparacionPropiedades(string file1, string file2)
{
if (compareFilesByProperties(file1, file2)==true)
{
Console.WriteLine("Los {0} y {1} son iguales por fecha de creación y por última modificación",file1, file2);
}
else
{
Console.WriteLine("Los archivos {0} y {1} no son iguales por fecha de creación y por última modificación", file1, file2);
}
}
According to what you show, you are comparing only the dates of the
DateTime
, without the hours, minutes, seconds or ticks, and I think that your problem comes from there.To be able to compare all that you must remove from your condition the
.Date
and compare theDateTime
integer:The problem you have is that you are not comparing the files that you create:
You are accessing the root directory of the directories you pass to the (
dirOrigen.RootDirectory
) method. Remove the first two lines (you don't needDriveInfo
them at all) and modify these lines to get the files from the directories you pass to the method: