file exists by file name pattern

前端 未结 3 1020
陌清茗
陌清茗 2020-11-30 04:27

I am using:

File.Exists(filepath)

What I would like to do is swop this out for a pattern, because the first part of the filename changes.<

相关标签:
3条回答
  • 2020-11-30 05:02

    Get a list of all matching files using System.IO.DirectoryInfo.GetFiles()

    Also see SO Questions:

    Is there a wildcard expansion option for .net apps?

    How do I check if a filename matches a wildcard pattern

    and many others...

    0 讨论(0)
  • 2020-11-30 05:15

    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
    }
    
    0 讨论(0)
  • 2020-11-30 05:15

    If you're using .net framework 4 or above you could use Directory.EnumerateFiles

    bool exist = Directory.EnumerateFiles(path, "*_peach.xml").Any();
    

    This could be more efficient than using Directory.GetFiles since you avoid iterating trough the entire file list.

    0 讨论(0)
提交回复
热议问题