File search method:
private List<string> GetFilesList(string path, string pattern) { List<string> fileList = new List<string>(); foreach (string fileName in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)) { if (pattern.Contains(Path.GetExtension(fileName))) { fileList.Add(fileName); } } return fileList; } Folder check method:
static long GetDirSize(string path) { long size = 0; string[] files = Directory.GetFiles(path); foreach (string file in files) size += (new FileInfo(file)).Length; string[] dirs = Directory.GetDirectories(path); foreach (string dir in dirs) size += GetDirSize(dir); return size; } Now I do a check:
if (GetDirSize(TxtPath) < 1000000) { // Если папка превышает размер: остановить метод GetFilesList(path,pattern); } else // Если не превышает продолжить метод пока папка не будет превышать заданный размер. The trouble is that I can't do everything together (using a while(true) , or on a timer.
PS: The point is to make a check, and stop the called method after exceeding the folder size.
PPS: I put the question in a slightly different form: When calling the method GetFilesList(path,pattern); - Files are copied into my folder (all files), it takes a long time and weight! Therefore, you need to check the size of the folder, and stop the execution of the function of the method).
GetDirSize) and get a list of files (GetFilesList) in parallel? And if the test is not worked out, then stop the process of obtaining a list of files. - Vlad