Hello! We need an approximate code that did a search in all folders except for certain ones, that is, for example, I scan the C: / drive. And it is necessary to exclude the C: / Windows folder in it, that is, that it exclude this folder and go according to the following, for example, C: / Games , but not C: / Windows itself .
Here is my code
public static IEnumerable<string> GetFiles(string path, string pattern) { IEnumerable<string> files = null; try { files = Directory.GetFiles(path, pattern); } catch { } if (files != null) { foreach (var file in files) yield return file; } IEnumerable<string> directories = null; try { directories = Directory.GetDirectories(path); } catch { } if (directories != null) { foreach (var file in directories.SelectMany(d => GetFiles(d, pattern))) { yield return file; } } } public void DirSearch(string sDir, string keywords) { List<string> DirList = new List<string>(); DirList.Add("C:\\Windows"); var s1 = FileSearch.GetFiles(sDir, "*.ini").Where(d => DirList.All(e => !d.StartsWith(e))); foreach (string file in s1) { if (FindKeywords(file, keywords)) // тут мой код, думаю не обязательно показывать { MessageBox.Show(file); } } }
Thank you in advance!