search pattern  
Author Message
Can-Ann





PostPosted: Visual C# Language, search pattern Top

I need to check for both .exe and .txt files in this statement

foreach (FileInfo f in dir.GetFiles( ))

Can anyone show me how to change the search pattern to include the .txt files



Visual C#11  
 
 
ahmedilyas





PostPosted: Visual C# Language, search pattern Top

you can't unfortunately. you will have to create an array which contains the extensions you wish to look for them iterate through each extension. Example:

string[] extensions = {"*.exe", "*.txt"};

foreach(string curExtension in extensions)

{

foreach(FileInfo f in dir.GetFiles("FolderPath", curExtension))

{

//do whatever

}

}



 
 
Can-Ann





PostPosted: Visual C# Language, search pattern Top

ahmedilyas-

again, many thanks.


 
 
ahmedilyas





PostPosted: Visual C# Language, search pattern Top

anytime :-)

 
 
James Curran





PostPosted: Visual C# Language, search pattern Top

Alternately, you can do the file selection manually:



foreach (FileInfo f in dir.GetFiles(
))
{
string ext = Path.GetExtension(f.Name).ToLower();
if (ext == ".txt" || ext == '.exe")
{

/// do stuff here
}

}