I’m using .NET 4.5 and C#. My code below works fine if the spelling is case sensitive. In other words if the file is spelled exactly like “SetupV8.exe”. But I really need it to be case insensitive. I’ve played with it but cant find a way.
foreach (string file in directory.EnumerateFiles((AppDomain.CurrentDomain.BaseDirectory),
"*.exe", SearchOption.AllDirectories))
{
if (!file.Contains("SetupV8.exe")
{
// Do something
}
}
Thanks
file.ToLower().Contains("setupv8.exe")usually works fine. (though you might want to consider EndsWith instead)Also, since
EnumerateFilesreturnsFileInfo, you might as well check itsNameproperty instead:Also, if the name is “SetupV8.exe” and you don’t expect it to be prefixed/suffixed with anything, perhaps just straight up check for equality at this point.
EDIT: Perhaps more importantly, you probably want to use just the file name. Unless you want to check if any part of the directory path matches. That is, you might not want
c:\temp\setupv8.exe_directory\subdirectory\setupv8.exeto match as a false positive.EDIT 8 years later for new readers: There are some edge cases where using
ToLower()can introduce some unexpected results, so perhaps it might be preferable to useToLowerInvariant()instead.