How to determine if a File Matches a File Mask?

后端 未结 13 1639
清歌不尽
清歌不尽 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:22

    From Windows 7 using P/Invoke (without 260 char count limit):

    // UNICODE_STRING for Rtl... method
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct UNICODE_STRING
    {
        public ushort Length;
        public ushort MaximumLength;
        [MarshalAs(UnmanagedType.LPWStr)]
        string Buffer;
    
        public UNICODE_STRING(string buffer)
        {
            if (buffer == null)
                Length = MaximumLength = 0;
            else
                Length = MaximumLength = unchecked((ushort)(buffer.Length * 2));
            Buffer = buffer;
        }
    }
    
    // RtlIsNameInExpression method from NtDll.dll system library
    public static class NtDll
    {
        [DllImport("NtDll.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
        [return: MarshalAs(UnmanagedType.U1)]
        public extern static bool RtlIsNameInExpression(
            ref UNICODE_STRING Expression,
            ref UNICODE_STRING Name,
            [MarshalAs(UnmanagedType.U1)]
            bool IgnoreCase,
            IntPtr Zero
            );
    }
    
    public bool MatchMask(string mask, string fileName)
    {
        // Expression must be uppercase for IgnoreCase == true (see MSDN for RtlIsNameInExpression)
        UNICODE_STRING expr = new UNICODE_STRING(mask.ToUpper());
        UNICODE_STRING name = new UNICODE_STRING(fileName);
    
        if (NtDll.RtlIsNameInExpression(ref expr, ref name, true, IntPtr.Zero))
        {
            // MATCHES !!!
        }
    }
    
    0 讨论(0)
  • 2020-12-14 07:23

    If PowerShell is available, it has direct support for wildcard type matching (as well as Regex).

    WildcardPattern pat = new WildcardPattern("a*.b*");
    if (pat.IsMatch(filename)) { ... }
    
    0 讨论(0)
  • 2020-12-14 07:24

    None of these answers quite seem to do the trick, and msorens's is needlessly complex. This one should work just fine:

    public static Boolean Fits(string sFileName, string sFileMask)
    {
        String convertedMask = "^" + Regex.Escape(sFileMask).Replace("\\*", ".*").Replace("\\?", ".") + "$";
        Regex regexMask = new Regex(convertedMask, RegexOptions.IgnoreCase);
        return regexMask.IsMatch(sFileName)
    }
    

    This makes sure possible regex chars in the mask are escaped, replaces the \* and \?, and surrounds it all by ^ and $ to mark the boundaries.

    Of course, in most situations, it's far more useful to simply make this into a FileMaskToRegex tool function which returns the Regex object, so you just got it once and can then make a loop in which you check all strings from your files list on it.

    public static Regex FileMaskToRegex(string sFileMask)
    {
        String convertedMask = "^" + Regex.Escape(sFileMask).Replace("\\*", ".*").Replace("\\?", ".") + "$";
        return new Regex(convertedMask, RegexOptions.IgnoreCase);
    }
    
    0 讨论(0)
  • 2020-12-14 07:25

    How about using reflection to get access to the function in the .NET framework?

    Like this:

    public class PatternMatcher
    {
      public delegate bool StrictMatchPatternDelegate(string expression, string name);
      public StrictMatchPatternDelegate StrictMatchPattern;
      public PatternMatcher()
      {
        Type patternMatcherType = typeof(FileSystemWatcher).Assembly.GetType("System.IO.PatternMatcher");
        MethodInfo patternMatchMethod = patternMatcherType.GetMethod("StrictMatchPattern", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
        StrictMatchPattern = (expression, name) => (bool)patternMatchMethod.Invoke(null, new object[] { expression, name });
      }
    }
    
    void Main()
    {
      PatternMatcher patternMatcher = new PatternMatcher();
      Console.WriteLine(patternMatcher.StrictMatchPattern("*.txt", "test.txt")); //displays true
      Console.WriteLine(patternMatcher.StrictMatchPattern("*.doc", "test.txt")); //displays false
    }
    
    0 讨论(0)
  • 2020-12-14 07:27

    I appreciate finding Joel's answer--saved me some time as well ! I did, however, have to make a few changes to make the method do what most users would expect:

    • I removed the 'this' keyword preceding the first argument. It does nothing here (though it could be useful if the method is intended to be an extension method, in which case it needs to be public and contained within a static class and itself be a static method).
    • I made the regular expression case-independent to match standard Windows wildcard behavior (so e.g. "c*.*" and "C*.*" both return the same result).
    • I added starting and ending anchors to the regular expression, again to match standard Windows wildcard behavior (so e.g. "stuff.txt" would be matched by "stuff*" or "s*" or "s*.*" but not by just "s").

    private bool FitsMask(string fileName, string fileMask)
    {
        Regex mask = new Regex(
            '^' + 
            fileMask
                .Replace(".", "[.]")
                .Replace("*", ".*")
                .Replace("?", ".")
            + '$',
            RegexOptions.IgnoreCase);
        return mask.IsMatch(fileName);
    }
    

    2009.11.04 Update: Match one of several masks

    For even more flexibility, here is a plug-compatible method built on top of the original. This version lets you pass multiple masks (hence the plural on the second parameter name fileMasks) separated by lines, commas, vertical bars, or spaces. I wanted it so that I could let the user put as many choices as desired in a ListBox and then select all files matching any of them. Note that some controls (like a ListBox) use CR-LF for line breaks while others (e.g. RichTextBox) use just LF--that is why both "\r\n" and "\n" show up in the Split list.

    private bool FitsOneOfMultipleMasks(string fileName, string fileMasks)
    {
        return fileMasks
            .Split(new string[] {"\r\n", "\n", ",", "|", " "},
                StringSplitOptions.RemoveEmptyEntries)
            .Any(fileMask => FitsMask(fileName, fileMask));
    }
    

    2009.11.17 Update: Handle fileMask inputs more gracefully

    The earlier version of FitsMask (which I have left in for comparison) does a fair job but since we are treating it as a regular expression it will throw an exception if it is not a valid regular expression when it comes in. The solution is that we actually want any regex metacharacters in the input fileMask to be considered literals, not metacharacters. But we still need to treat period, asterisk, and question mark specially. So this improved version of FitsMask safely moves these three characters out of the way, transforms all remaining metacharacters into literals, then puts the three interesting characters back, in their "regex'ed" form.

    One other minor improvement is to allow for case-independence, per standard Windows behavior.

    private bool FitsMask(string fileName, string fileMask)
    {
        string pattern =
             '^' + 
             Regex.Escape(fileMask.Replace(".", "__DOT__")
                             .Replace("*", "__STAR__")
                             .Replace("?", "__QM__"))
                 .Replace("__DOT__", "[.]")
                 .Replace("__STAR__", ".*")
                 .Replace("__QM__", ".")
             + '$';
        return new Regex(pattern, RegexOptions.IgnoreCase).IsMatch(fileName);
    }
    

    2010.09.30 Update: Somewhere along the way, passion ensued...

    I have been remiss in not updating this earlier but these references will likely be of interest to readers who have made it to this point:

    • I embedded the FitsMask method as the heart of a WinForms user control aptly called a FileMask--see the API here.
    • I then wrote an article featuring the FileMask control published on Simple-Talk.com, entitled Using LINQ Lambda Expressions to Design Customizable Generic Components. (While the method itself does not use LINQ, the FileMask user control does, hence the title of the article.)
    0 讨论(0)
  • 2020-12-14 07:32

    Nissim mentioned the PatternMatcher Class in his answer...

    There is an explanation available here:

    http://referencesource.microsoft.com/#System/services/io/system/io/PatternMatcher.cs

    So you don't have to use the reflected code and guess how it works.

    Also, I think using this code is probably the best solution, because it guarantees consistent behavior when using the same pattern in your comparisons and in Framework methods like GetFiles().

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