匹配“..\ThirdParty\dlls\*.dll”的文件名

发布于 2024-12-07 16:04:20 字数 597 浏览 5 评论 0原文

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

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

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

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

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

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

Is there an easy way to get a list of filenames that matach a filename pattern including references to parent directory? What I want is for "..\ThirdParty\dlls\*.dll" to return a collection like ["..\ThirdParty\dlls\one.dll", "..\ThirdParty\dlls\two.dll", ...]

I can find several questions relating matching files names including full path, wildcards, but nothing that includes "..\" in the pattern. Directory.GetFiles explicitly disallows it.

What I want to do with the names is to include them in a zip archive, so if there is a zip library that can understand relative paths like this I am happier to use that.

The pattern(s) are coming from an input file, they are not known at compile time. They can get quite complex, e.g ..\src\..\ThirdParty\win32\*.dll so parsing is probably not feasible.

Having to put it in zip is also the reason I am not very keen on converting the pattern to fullpath, I do want the relative paths in zip.

EDIT: What I am looking for really is a C# equivalent of /bin/ls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-12-14 16:04:20
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;
}
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;
}
错々过的事 2024-12-14 16:04:20

Path.GetFullPath() 函数可以将相对路径转换为绝对路径。您可以在路径部分使用它。

string pattern = @"..\src\..\ThirdParty\win32\*.dll";

string relativeDir = Path.GetDirectoryName(pattern);
string absoluteDir = Path.GetFullPath(relativeDir);
string filePattern = Path.GetFileName(pattern);

foreach (string file in Directory.GetFiles(absoluteDir, filePattern))
{

}

There is the Path.GetFullPath() function that will convert from relative to absolute. You could use it on the path part.

string pattern = @"..\src\..\ThirdParty\win32\*.dll";

string relativeDir = Path.GetDirectoryName(pattern);
string absoluteDir = Path.GetFullPath(relativeDir);
string filePattern = Path.GetFileName(pattern);

foreach (string file in Directory.GetFiles(absoluteDir, filePattern))
{

}
你怎么这么可爱啊 2024-12-14 16:04:20

如果我理解正确,您可以将 Directory.EnumerateFiles 与这样的正则表达式结合使用(但我还没有测试过):

var matcher = new Regex(@"^\.\.\\ThirdParty\\dlls\\[^\\]+.dll$");
foreach (var file in Directory.EnumerateFiles("..", "*.dll", SearchOption.AllDirectories)
{
    if (matcher.IsMatch(file))
        yield return file;
}

If I understand you correctly you could use Directory.EnumerateFiles in combination with a regular expression like this (I haven't tested it though):

var matcher = new Regex(@"^\.\.\\ThirdParty\\dlls\\[^\\]+.dll$");
foreach (var file in Directory.EnumerateFiles("..", "*.dll", SearchOption.AllDirectories)
{
    if (matcher.IsMatch(file))
        yield return file;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文