Reputation: 2025
So, I make a program to make a backup of some particular file, with particular extensions, so, I enter with List or array with the extensions of I whant to make a backup
List<string> extensions = new List<string>();
extensions.Add("*.pdf");
extensions.Add("*.txt");
extensions.Add("*.inf");
extensions.Add("*.doc");
extensions.Add("*.cpp");
extensions.Add("*.cs");
extensions.Add("*.vb");
Ok, but, how I can make the search system, to find the files with that extensions in folders..
the search system is simple:
public void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, "*.pdf"))
{
Console.WriteLine(f);
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
Ok, but, how I can make this to search for all extensions in list ( to make a most rapid system ), and, the program can not enter on windows folder..., if I set the sDir = "C:\"
Upvotes: 1
Views: 1340
Reputation: 75306
Use overload GetFile
with option SearchOption.AllDirectories
, so you don't need to call recursively, also use LINQ with SelectMany
:
var result = extensions.SelectMany(e =>
Directory.GetFiles(sDir, e, SearchOption.AllDirectories));
Update: To ignore protected folder, you can use try catch
to skip exception:
private string[] GetFiles(string directory, string pattern)
{
try
{
return Directory.GetFiles(directory, pattern,
SearchOption.AllDirectories);
}
catch (Exception)
{
return new string[0];
}
}
So:
var result = extensions.SelectMany(e => GetFiles(sDir, e));
Upvotes: 1
Reputation: 29953
It may be slightly more performant to call GetFiles(string Directory) and get the results back into a single list, and then parse that. The following snippet should do what you need...
var extensions = new List<string> { ".pdf", ".txt", ".inf", ".doc", ".cpp", ".cs", ".vb" };
var files = Directory.GetFiles(topLevelFolder, "*.*", SearchOption.AllDirectories);
var matching = new List<string>();
foreach (var ext in extensions)
{
matching.AddRange(files.Where(f => f.EndsWith(ext)));
}
Upvotes: 0
Reputation: 25435
You can supply multiple search criteria but seperating the value with a semi-colon
*.vb;*.txt
So you can simply generate the search string using the list
var search = string.Join(";", extensions.ToArray());
...
foreach (string f in Directory.GetFiles(d, search))
{
...
}
Upvotes: 0
Reputation: 13033
You don't need an extra loop or recursion, just use overloaded Directory.GetFiles
which will get all file names recursively
public void DirSearch(string sDir)
{
try
{
foreach (string item in extensions)
{
string[] files = Directory.GetFiles(sDir, item, SearchOption.AllDirectories);
foreach (var file in files)
{
Console.WriteLine(file);
}
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
Upvotes: 0
Reputation: 45083
To simply extend what you have you're going to need another loop, one that iterates the extensions and calls GetFiles
based on the current, though "to make a most rapid system" is highly ambitious at this level. Anyway,...
foreach (string d in Directory.GetDirectories(sDir)) {
foreach (string e in extensions) {
foreach (string f in Directory.GetFiles(d, e)) {
}
}
}
Upvotes: 1