I have to test that when looking for a file by name in a directory it does not exist and it indicates it to me on the screen. I show you my code, where the question mark comment is where I have the problem, the rest works correctly:
static void recorrerDirectoriosYCompararPorFicheros(string dir1, string dir2)
{
System.IO.DirectoryInfo dirOrigenInfo = new DirectoryInfo(dir1);
System.IO.DirectoryInfo dirDestinoInfo = new DirectoryInfo(dir2);
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.FullName, file2.FullName);
}
else//????????????????????
{
Console.WriteLine("\nEl archivo {0} no existe en {1}", file.Name, dir2);
}
}
}
}
Here I leave the result, it tells me comparison by comparison if it exists or not, what I want is for the directory to be traversed and tell me only once if it exists or not, the file that does not exist is c.txt
: Date of Creation or last modification NO are the same The files C:\Projects\Folder\a.txt and C:\Projects\Folder taDestino\a.txt are not the same by date of creation and by last modification
The a.txt file does not exist in C:\Projects\DestinationFolder
The a.txt file does not exist in C:\Projects\DestinationFolder
The a.txt file does not exist in C:\Projects\DestinationFolder
The c.txt file does not exist in C:\Projects\DestinationFolder
The c.txt file does not exist in C:\Projects\DestinationFolder
Creation date and last modification are the same The C:\Projects\FolderSource\c.txt and C:\Projects\FolderDestin o\c.txt are the same by date of creation and by last modification
The c.txt file does not exist in C:\Projects\DestinationFolder
Configuration.ini file does not exist in C:\Projects\DestinationFolder
Configuration.ini file does not exist in C:\Projects\DestinationFolder
Configuration.ini file does not exist in C:\Projects\DestinationFolder
Date of Creation or last modification are NOT the same The files C:\Projects\FolderFolder\Configuration.ini and C:\Projects\FolderDestination\Configuration.ini are not the same by date of creation and by date of last modification
In your code you are taking each file from the source directory and comparing it to each existing file in the destination directory. In each comparison, if the name doesn't match, you conclude that the file doesn't exist (mistakenly because it doesn't match the first file doesn't mean it won't match another) and yet you keep looking.
What you should do is search all the destination files for the name of the source file and, at the end, conclude if it has been found or not.
To simplify your code you can use LINQ instead of nested loops:
I would use Linq and
Contains
to check that the name you're parsing doesn't exist in the target collection: