Reputation:
I am trying to remove all the elements from a string array that contains a specific value. In this case, my string array contains bunch of files with their folder structure that will be zipped on a later operation. I need to exclude specific files that contains lets say "CDN" values.
Example string structure would be like this
C:\Production\Websites\WebSite1\index.html
C:\Production\Websites\WebSite1\product.html
C:\Production\Websites\CDN\content.html
C:\Production\Websites\CDN\frame.html
C:\Production\Websites\WebSite2\index.html
C:\Production\Websites\WebSite2\product.html
Values are just example... But I need to remove elements from the list completely from the list that contains "\CDN\".
This is how I get the folders.
string[] files = Directory.GetFiles(@"C:\Production\Websites", "*.*", SearchOption.AllDirectories);
I tried something like this, but apparently I got stuck...
string[] files = Directory.GetFiles(@"C:\Production\Websites", "*.*", SearchOption.AllDirectories).Where(s => !s.Contains("CDN"))....???
Any ideas? Cheers
Upvotes: 0
Views: 1123
Reputation: 35733
from MSDN
The
EnumerateFiles
andGetFiles
methods differ as follows: When you useEnumerateFiles
, you can start enumerating the collection of names before the whole collection is returned; when you useGetFiles
, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories,EnumerateFiles
can be more efficient.
so: enumerate files, filter sequence and make a resulting array
string[] files = Directory.EnumerateFiles(@"C:\Production\Websites", "*.*", SearchOption.AllDirectories)
.Where(s => !s.Contains("CDN"))
.ToArray();
Upvotes: 2