문제

fileNameMaskI need load files from directory by filename mask, if filename begin with "Table 1" work well, but if directory contain any files: "TestTable 1 someValue.xlsx" or "Test Table 1 someValue.xlsx" my mask not work.

If I change mask to "^(Table 1).*\.xlsx$" - Directory.GetFiles return all files with "Table 1", but I need return only files where file name start from "Table 1".

My test code:

 var fileNameMask = "(Table 1).*\\.xlsx";
 string path = @"c:\Temp\";
 Regex searchPattern = new Regex(fileNameMask);
 string[] files = Directory.GetFiles(path).Where(f => searchPattern.IsMatch(f)).ToArray(); 
도움이 되었습니까?

해결책 2

Try this mask:

string maskfile = @"[^\w\s](Table 1)(.*)(.xlsx)"; 

다른 팁

Have you tried to use built-in functionality of GetFiles method?

Directory.GetFiles(path, "Test 1*.xlsx")

Try code below:

private String[] GetFilesByMask(string pPath)
{
    Regex mask = new Regex(sFileMask.Replace(".", "[.]").Replace("*", ".*").Replace(".", "."));
    Return Directory.GetFiles(pPath).Where(f => mask.IsMatch(f)).ToArray();

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top