Currently I am using the following code to search for files in a folder:
public string[] getFiles(string SourceFolder, string Filter,System.IO.SearchOption searchOption)
{
// ArrayList will hold all file names
ArrayList alFiles = new ArrayList();
// Create an array of filter string
string[] MultipleFilters = Filter.Split('|');
// for each filter find mathing file names
foreach (string FileFilter in MultipleFilters)
{
// add found file names to array list
alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption));
}
// returns string array of relevant file names
return (string[])alFiles.ToArray(typeof(string));
}
The problem is that when I pass a drive like D:\\
as the path to search, either I get an exception in GetFiles()
or nothing is found!
I also get exceptions when I try to access some hidden or system secured folder.
How can I properly search for files in a drive or folder recursively?
One more thing, I come to know that a extension like "abc" may return files with having extensions like "abcd" or "abcde".
If this is true, how can I overcome this problem?
Thank you.