有没有一种简单的方法来获取与文件名模式匹配的文件名列表包括对父目录的引用?我想要的是 "..\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/