有誰知道在.NET中是否有與Windows API函數PathMatchSpec()
等效的功能?等效於.NET的PathMatchSpec
1
A
回答
0
如果你不能使用正則表達式獲得功能(我相信是這種情況)如何通過PInvoke使用PathMatchSpec()?
http://www.pinvoke.net/default.aspx/shlwapi/PathMatchSpec.html
0
你可以試試 How to implement glob in C# 或在必要時當然還有的PInvoke route。
0
我不知道內置於.NET的方法不過是微不足道用正則表達式來複制:
public static bool PathMatchSpec(String path, String spec)
{
String specAsRegex = Regex.Escape(spec).Replace("\\*", ".*").Replace("\\?", ".") + "$";
return Regex.IsMatch(path, specAsRegex);
}
顯然,這種假設System.Text.RegularExpressions命名空間被引用。如果你打算用相同的規格做很多事情,你也可以緩存正則表達式。
編輯添加:P/Invoke確實是一個選項,但PathMatchSpec的簽名指示它需要一個ANSI字符串,所以您會爲每個調用引發字符集轉換。請記住,如果你走這條路。在這種情況下,PathMatchSpecEx可能會更好。
0
總之......不,我知道的......但也許這可以幫助你一起(注意,一個更長一點比你可能想,但它使我受益匪淺):
sealed public class WildcardMatch
{
private static Regex wildcardFinder = new Regex(@"(?<wildcards>\?+|\*+)", RegexOptions.Compiled | RegexOptions.Singleline);
private Regex wildcardRegex;
public WildcardMatch(string wildcardFormat) : this(wildcardFormat, false) { }
public WildcardMatch(string wildcardFormat, bool ignoreCase)
{
if (wildcardFormat == null)
throw new ArgumentNullException("wildcardFormat");
StringBuilder patternBuilder = new StringBuilder("^");
MatchCollection matches = this.wildcardFinder.Matches(wildcardFormat);
string[] split = this.wildcardFinder.Split(wildcardFormat);
for (int ix = 0; ix < split.Length; ix++)
{
// Even indexes are literal text, odd indexes correspond to matches
if (ix % 2 == 0)
patternBuilder.Append(Regex.Escape(split[ix]));
else
{
// Matches must be substituted with Regex control characters
string wildcards = matches[ix/2].Groups["wildcards"].Value;
if (wildcards.StartsWith("*", StringComparison.Ordinal))
patternBuilder.Append("(.*)");
else
patternBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", wildcards.Replace('?', '.'));
}
}
patternBuilder.Append("$");
this.wildcardRegex = new Regex(
patternBuilder.ToString(),
RegexOptions.Singleline | (ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None));
}
public bool IsMatch(string value)
{
if (value == null)
return false;
return this.wildcardRegex.IsMatch(value);
}
public IEnumerable<string> ExtractMatches(string value)
{
if (value == null)
yield break;
Match match = this.wildcardRegex.Match(value);
if (!match.Success)
yield break;
for (int ix = 1; ix < match.Groups.Count; ix++)
yield return match.Groups[ix].Value;
}
}
+0
請注意,通過使用anelsons的Regex.Escape()'轉義代碼可以絕對簡化。 – jerryjvl 2009-06-05 06:15:51
相關問題
- 1. ShellExecute等效於.NET
- 2. 等效於.NET中的NSOperationQueue
- 3. 等效於.NET的MappedBytesBuffer?
- 4. KeyStorage .Net等效於Java?
- 5. .Net等效於ScaleHeight和Scalewidth
- 6. .net int()等效於PHP
- 7. Cobertura等效於C#.NET嗎?
- 8. protobuf-net等效於StreamingContext
- 9. .net等效於php preg_replace
- 10. Java中的TreeSet等效於C#.net
- 11. Python中的urllib等效於.net
- 12. 等效於.NET的構造函數
- 13. .NET JAXB等效?
- 14. SQL十進制等效於.NET
- 15. 蟒蛇扭曲等效於.net
- 16. ApiControllerActionSelector等效於Asp .net MVC核心
- 17. Mono等效於.NET Framework調試控制
- 18. Worklow Foundation(.NET)等效於Python,Java,C++嗎?
- 19. 「開始/親和力」等效於.NET
- 20. .Net等效於x86 ASM命令XADD
- 21. .Net等效於vb6 Tab控件範例
- 22. 是否有一個JProfiler等效於.NET?
- 23. 是否存在.net TypeConverter等效於Java
- 24. .NET 4.0中.NET 4.5的Task.FromResult()的等效
- 25. 與GetLastInputInfo等效的.NET?
- 26. .net中的DLast等效
- 27. .NET 2.0的等效TimeZoneInfo.ConvertTime?
- 28. .NET IoC - .NET Unity的等效代碼
- 29. 等效於'@ECHO ON'的Unix等效
- 30. 等效於OpenSSL的
酷......我自己並不知道Escape()方法;肯定會簡化我的解決方案的一部分;) – jerryjvl 2009-06-05 06:15:13