System.IO.FileInfo.Equals() is not returning expected result in Where LINQ extension method

636 Views Asked by At

When attempting to return all other files from the same directory that a specified file resides in, the files being returned are including the specified file. Lets assume I have three files out in a server share on my network:

  • \\Server\Share\Path\To\file1.ext
  • \\Server\Share\Path\To\file2.ext
  • \\Server\Share\Path\To\file3.ext

When I attempt to filter out file2 using the .Equals() method on the FileInfo object, the file is still being included.

FileInfo theFile = new FileInfo(@"\\Server\Share\Path\To\file2.ext");
List<FileInfo> allOtherFilesList = theFile.Directory.EnumerateFiles("*").Where(f => !f.Equals(vidFile)).ToList();

//Returned files:
\\Server\Share\Path\To\file1.ext
\\Server\Share\Path\To\file2.ext
\\Server\Share\Path\To\file3.ext

If I use the .Equals() method on the FileInfo.FullName property, I am returned a list of files as I would expect

FileInfo theFile = new FileInfo(@"\\Server\Share\Path\To\file2.ext");
List<FileInfo> allOtherFilesList = theFile.Directory.EnumerateFiles("*").Where(f => !f.FullName.Equals(vidFile.FullName)).ToList();

//Returned files:
\\Server\Share\Path\To\file1.ext
\\Server\Share\Path\To\file3.ext

Surely, there is some aspect of the .Equals() method that I'm not understanding? I would have thought that two FileInfo objects representing the same file would be the same.

0

There are 0 best solutions below