I am using the following line of code to get a list of all the files under an entered path:
Files = Directory.GetFiles(path, ".", SearchOption.AllDirectories);
However, what I want to do is, rather than getting all files, I want to exclude any that have certain file extensions. I am reading the list of file extensions to ignore from a text file which has one file extension per line (".pdf", ".dll", etc). I am using the following code to load the list of file extensions to ignore from the text file:
ArrayList line = new ArrayList();
using (StreamReader reader = new StreamReader(Server.MapPath("~/TextFile.txt")))
{
while (!reader.EndOfStream)
{
line.Add(reader.ReadLine());
}
}
My question is, how do I now limit my file search to not include any files that match any of those file extensions? I don't want to add those types of files into my Files
string array.
You cannot specify a list of file extensions to exclude, so you will just have to get the full list and filter them out yourself. For instance, something like this should work:
List<string> fileExtensionsToIgnore = new List<String>(File.ReadAllLines("~/TextFile.txt"));
List<string> fileList = new List<string>();
foreach (string filePath in Directory.GetFiles(Path, ".", SearchOption.AllDirectories))
{
if (!fileExtensionsToIgnore.Contains(Path.GetExtension(filePath).ToLower())
fileList.Add(filePath);
}
string[] files = fileList.ToArray();