How to determine if a File Matches a File Mask?

后端 未结 13 1702
清歌不尽
清歌不尽 2020-12-14 07:14

I need to decide whether file name fits to file mask. The file mask could contain * or ? characters. Is there any simple solution for this?

bool bFits = Fits         


        
13条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 07:49

    For .net Core the way microsoft does.

            private bool MatchPattern(ReadOnlySpan relativePath)
            {
                ReadOnlySpan name = IO.Path.GetFileName(relativePath);
                if (name.Length == 0)
                    return false;
    
                if (Filters.Count == 0)
                    return true;
    
                foreach (string filter in Filters)
                {
                    if (FileSystemName.MatchesSimpleExpression(filter, name, ignoreCase: !PathInternal.IsCaseSensitive))
                        return true;
                }
    
                return false;
            }
    

    The way microsoft itself seemed to do for .NET 4.6 is documented in github:

        private bool MatchPattern(string relativePath) {            
            string name = System.IO.Path.GetFileName(relativePath);            
            if (name != null)
                return PatternMatcher.StrictMatchPattern(filter.ToUpper(CultureInfo.InvariantCulture), name.ToUpper(CultureInfo.InvariantCulture));
            else
                return false;                
        }
    

提交回复
热议问题