如何在C#中实现glob

我不知道在StackOverflow上发布你自己的问题答案是否合法,但我看到没有人问过这个问题。 我去寻找一个C#Glob并没有找到一个,所以我写了一个其他人可能觉得有用的。

///  /// return a list of files that matches some wildcard pattern, eg /// C:\p4\software\dotnet\tools\*\*.sln to get all tool solution files ///  /// pattern to match /// all matching paths public static IEnumerable Glob(string glob) { foreach (string path in Glob(PathHead(glob) + DirSep, PathTail(glob))) yield return path; } ///  /// uses 'head' and 'tail' -- 'head' has already been pattern-expanded /// and 'tail' has not. ///  /// wildcard-expanded /// not yet wildcard-expanded ///  public static IEnumerable Glob(string head, string tail) { if (PathTail(tail) == tail) foreach (string path in Directory.GetFiles(head, tail).OrderBy(s => s)) yield return path; else foreach (string dir in Directory.GetDirectories(head, PathHead(tail)).OrderBy(s => s)) foreach (string path in Glob(Path.Combine(head, dir), PathTail(tail))) yield return path; } ///  /// shortcut ///  static char DirSep = Path.DirectorySeparatorChar; ///  /// return the first element of a file path ///  /// file path /// first logical unit static string PathHead(string path) { // handle case of \\share\vol\foo\bar -- return \\share\vol as 'head' // because the dir stuff won't let you interrogate a server for its share list // FIXME check behavior on Linux to see if this blows up -- I don't think so if (path.StartsWith("" + DirSep + DirSep)) return path.Substring(0, 2) + path.Substring(2).Split(DirSep)[0] + DirSep + path.Substring(2).Split(DirSep)[1]; return path.Split(DirSep)[0]; } ///  /// return everything but the first element of a file path /// eg PathTail("C:\TEMP\foo.txt") = "TEMP\foo.txt" ///  /// file path /// all but the first logical unit static string PathTail(string path) { if (!path.Contains(DirSep)) return path; return path.Substring(1 + PathHead(path).Length); } 

我偶然发现了含有漂亮整齐的Glob类的铁ruby。 从相关代码中提取它相当容易。

https://github.com/IronLanguages/main/blob/master/Languages/Ruby/Ruby/Builtins/Glob.cs

您可以使用C#中的“dir”(又名“Get-ChildItem”)powershell cmdlet。
(我不是说你是否应该这样做。)

您必须手动将此引用添加到项目文件(“ .csproj”或“ .vcproj”):

  

有关如何使用C#中的cmdlet的详细信息,请参阅此处: http : //www.devx.com/tips/Tip/42716

这是一个工作计划:

 using System; using System.Collections.Generic; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Collections.ObjectModel; namespace CsWildcard { class Program { static IEnumerable CmdletDirGlobbing(string basePath, string glob){ Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); // cd to basePath if(basePath != null){ Pipeline cdPipeline = runspace.CreatePipeline(); Command cdCommand = new Command("cd"); cdCommand.Parameters.Add("Path", basePath); cdPipeline.Commands.Add(cdCommand); cdPipeline.Invoke(); // run the cmdlet } // run the "dir" cmdlet (eg "dir C:\*\*\*.txt" ) Pipeline dirPipeline = runspace.CreatePipeline(); Command dirCommand = new Command("dir"); dirCommand.Parameters.Add("Path", glob); dirPipeline.Commands.Add(dirCommand); Collection dirOutput = dirPipeline.Invoke(); // for each found file foreach (PSObject psObject in dirOutput) { PSMemberInfoCollection a = psObject.Properties; // look for the full path ("FullName") foreach (PSPropertyInfo psPropertyInfo in psObject.Properties) { if (psPropertyInfo.Name == "FullName") { yield return psPropertyInfo.Value.ToString(); // yield it } } } } static void Main(string[] args) { foreach(string path in CmdletDirGlobbing(null,"C:\\*\\*\\*.txt")){ System.Console.WriteLine(path); } foreach (string path in CmdletDirGlobbing("C:\\", "*\\*\\*.exe")) { System.Console.WriteLine(path); } Console.ReadKey(); } } }