Search code examples
c#.net-2.0.net

File exists by file name pattern


I am using:

File.Exists(filepath)

I would like to swap this out for a pattern, because the first part of the filename changes.

For example: the file could be

01_peach.xml
02_peach.xml
03_peach.xml

How can I check if the file exists based on some kind of search pattern?


Solution

  • You can do a directory list with a pattern to check for files

    string[] files = System.IO.Directory.GetFiles(path, "*_peach.xml", System.IO.SearchOption.TopDirectoryOnly);
    if (files.Length > 0)
    {
        //file exist
    }