我不知道在StackOverflow中是否合法發佈自己的問題答案,但是我看到沒有人問過這個問題。我去找了一個C#Glob,但沒有找到一個,所以我寫了一個其他人可能會覺得有用的東西。如何在C#中實現glob#
19
A
回答
14
/// <summary>
/// return a list of files that matches some wildcard pattern, e.g.
/// C:\p4\software\dotnet\tools\*\*.sln to get all tool solution files
/// </summary>
/// <param name="glob">pattern to match</param>
/// <returns>all matching paths</returns>
public static IEnumerable<string> Glob(string glob)
{
foreach (string path in Glob(PathHead(glob) + DirSep, PathTail(glob)))
yield return path;
}
/// <summary>
/// uses 'head' and 'tail' -- 'head' has already been pattern-expanded
/// and 'tail' has not.
/// </summary>
/// <param name="head">wildcard-expanded</param>
/// <param name="tail">not yet wildcard-expanded</param>
/// <returns></returns>
public static IEnumerable<string> 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;
}
/// <summary>
/// shortcut
/// </summary>
static char DirSep = Path.DirectorySeparatorChar;
/// <summary>
/// return the first element of a file path
/// </summary>
/// <param name="path">file path</param>
/// <returns>first logical unit</returns>
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];
}
/// <summary>
/// return everything but the first element of a file path
/// e.g. PathTail("C:\TEMP\foo.txt") = "TEMP\foo.txt"
/// </summary>
/// <param name="path">file path</param>
/// <returns>all but the first logical unit</returns>
static string PathTail(string path)
{
if (!path.Contains(DirSep))
return path;
return path.Substring(1 + PathHead(path).Length);
}
2
我偶然發現了包含一個漂亮整潔的Glob類的鐵紅寶石。從相關代碼中提取相當容易。
https://github.com/IronLanguages/main/blob/master/Languages/Ruby/Ruby/Builtins/Glob.cs
0
您可以使用 「目錄」(又名 「GET-ChildItem」)PowerShell命令從C#。
(我不是說你是否應該。)
你必須將此引用添加到您的項目文件( 「的.csproj」 或 「的.vcproj」)手動:
<Reference Include="System.Management.Automation" />
見在這裏,瞭解如何使用cmdlet從C#的詳細信息: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<string> 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 (e.g. "dir C:\*\*\*.txt")
Pipeline dirPipeline = runspace.CreatePipeline();
Command dirCommand = new Command("dir");
dirCommand.Parameters.Add("Path", glob);
dirPipeline.Commands.Add(dirCommand);
Collection<PSObject> dirOutput = dirPipeline.Invoke();
// for each found file
foreach (PSObject psObject in dirOutput) {
PSMemberInfoCollection<PSPropertyInfo> 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();
}
}
}
相關問題
- 1. vtable如何在C++和c#中實現?
- 2. 如何在C或C++中實現SDP?
- 3. 如何實現在C#
- 4. 如何使用regexp在C/C++中執行glob(Linux)
- 5. 如何在C++中提供實現?
- 6. 如何才能在match.success C#中實現?
- 7. 如何在C#中實現malloc操作#
- 8. 如何在C#中實現八叉樹?
- 9. 如何在C++中實現矢量
- 10. 如何在C#中實現多態#
- 11. 如何在xslt中實現「uniq -c」?
- 12. 如何在C++中實現回調?
- 13. 如何在C中實現註釋?
- 14. 如何在c中實現Python socket.unwrap()?
- 15. 如何在C++中實現類組合?
- 16. 如何在C#中實現YouTrack REST API#
- 17. 如何在C#.NET中實現CopyMemory(VB6)?
- 18. 如何在C#中實現Web服務
- 19. 如何在C++ crossplatform snprintf中實現?
- 20. 在C#中如何實現SIP封裝
- 21. 如何在C#中實現Javascript的XmlHttpRequest?
- 22. 在C++中如何臨時實現?
- 23. 如何在c#中實現java反射?
- 24. 如何在C#中實現String.Length?
- 25. List.Clear()如何在C#中實現?
- 26. 如何在c#中實現Array.ConvertAll方法
- 27. 如何在C中實現位集合
- 28. static_cast是如何在C++中實現的?
- 29. 如何在Android中實現C++向量?
- 30. 如何在C#中實現序列化?
在谷歌之後,我發現了glob應該做的事情。 http://en.wikipedia.org/wiki/Glob_(programming) – tuinstoel 2008-12-29 20:41:01
如果你還沒有成爲一個社區維基,你會得到更多的積分。 :-) – 2008-12-30 01:10:36