I'm tring the test to see if a DirectoryInfo[]
contains a directory
my code is below
DirectoryInfo[] test = dir.GetDirectories();
if(test.Contains(new DirectoryInfo(dir.FullName +"\\"+ "Test_Folder")))
{
ContainsTestFolder = true;
}
To me this should work but it does not seem to return true when it should. Any ideas to what I am doing wrong.
Use Enumerable.Any
DirectoryInfo[] test = dir.GetDirectories();
if (test.Any(r => r.FullName.Equals(Path.Combine(dir.FullName,"Test_Folder"))))
{
ContainsTestFolder = true;
}
The reason you are not getting the desired result is, Contains compare object reference, not its values. Also consider using Path.Combine instead of concatenating paths.