[英]File names matching “..\ThirdParty\dlls\*.dll”
是否有一種簡單的方法來獲取與文件名模式匹配的文件名列表, 包括對父目錄的引用 ? 我想要的是讓"..\\ThirdParty\\dlls\\*.dll"
返回["..\\ThirdParty\\dlls\\one.dll", "..\\ThirdParty\\dlls\\two.dll", ...]
我可以找到一些與匹配文件名有關的問題,包括完整路徑,通配符,但沒有任何內容在模式中包含“ .. \\”。 Directory.GetFiles
明確禁止使用它。
我要使用的名稱是將它們包含在zip存檔中 ,因此,如果有一個zip庫可以理解這樣的相對路徑,我會更樂於使用。
這些模式來自輸入文件,在編譯時未知。 它們可能變得非常復雜,例如..\\src\\..\\ThirdParty\\win32\\*.dll
因此解析可能不可行。
不得不將其放入zip也是我不太熱衷於將模式轉換為fullpath的原因,我確實想要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;
}
有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))
{
}
如果我理解正確,則可以將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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.