c# - 文件名匹配 "..\ThirdParty\dlls\*.dll"

标签 c# filenames glob

有没有一种简单的方法来获取与文件名模式匹配的文件名列表包括对父目录的引用?我想要的是 "..\ThirdParty\dlls\*.dll" 返回一个像 ["..\ThirdParty\dlls\one.dll", "..\ThirdParty\dlls\two.dll", ...]

我可以找到几个与匹配文件名相关的问题,包括完整路径、通配符,但没有任何内容包含模式中的“..\”。 Directory.GetFiles 明确禁止它。

我想对这些名称做的是将它们包含在一个zip 存档 中,所以如果有一个 zip 库可以像这样理解相对路径,我会更乐意使用它。

模式来自一个输入文件,它们在编译时是未知的。它们可能变得非常复杂,例如 ..\src\..\ThirdParty\win32\*.dll 因此解析可能不可行。

必须将它放在 zip 中也是我不太热衷于将模式转换为完整路径的原因,我确实想要 zip 中的相对路径。

编辑:我正在寻找的实际上是/bin/ls 的 C# 等价物。

最佳答案

static string[] FindFiles(string path)
{
    string directory = Path.GetDirectoryName(path); // seperate directory i.e. ..\ThirdParty\dlls
    string filePattern = Path.GetFileName(path); // seperate file pattern i.e. *.dll

    // if path only contains pattern then use current directory
    if (String.IsNullOrEmpty(directory))
        directory = Directory.GetCurrentDirectory();

    //uncomment the following line if you need absolute paths
    //directory = Path.GetFullPath(directory); 

    if (!Directory.Exists(directory))      
        return new string[0];

    var files = Directory.GetFiles(directory, filePattern); 
    return files;
}

关于c# - 文件名匹配 "..\ThirdParty\dlls\*.dll",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7618919/

相关文章:

html - 未加载特定文件夹名称的 CSS 文件

python - 使用 python glob 忽略 Windows 隐藏文件

c# - 评论不能放在括号内

c# - asp.net 网站(开发)第一次加载需要很长时间(C#/Visual Studio 2012)

c# - 向 ADO.NET 实体数据模型生成的类中的属性添加属性

android - 如何使用 Android 应用程序重命名 sdcard 上的文件?

filenames - 如何匹配相似的文件名并重命名,以便像Beyond Compare这样的差异工具将它们视为一对来执行二进制比较?

bash - 文件通配无需分词?

bash - 扩展的 glob 没有按预期扩展

c# - 删除数据表中的主键