Search code examples
c#pattern-matchingfilenames

Compare files by names in c#


I have a routine that should compare files within a folder that have the following pattern:

path_to_file/some_common_base*.ext

where path_to_file, common_base and .ext must match, but the * part may change.

Now the problem I get is that in I might have path_to_file being with forward or backward slashes (C:\\tmp\\... or C:/tmp/...).

What is the suggested way to compare if a file matches the required pattern or not, in terms of performance and simplicity?


Solution

  • Thanks to the above comment, I ended up using the following approach, comparing the file names (which works as I know extensions):

    string testingFile = "....";
    DirectoryInfo dir = new DirectoryInfo("path_to_file");
    foreach (FileInfo f in dir.GetFiles("*.ext")) {
        if (f.Name.ToUpperInvariant().StartsWith("some_common_base")) {
           // f is mathing requirement...
        }
    }