Getting all the files (including files in sub-folders) of the given file extensions inside a folder in C#


This weekend was working on a problem which required to get the list of all the files including files inside sub-folders for the given list of extensions. So thought of writing a simple utility helper method to do the same.

Please note since we are initially getting all the files and then doing to LINQ to filter, only use at places number of files are not going to be very large.

 

 image

 

Enjoy!!!

Advertisements

MEF Example Part II – VS2010 and .NET 4.0


I got few comments requesting to have the same sample in VS2010 and .NET 4.0. I also did some enhancement to folder structure and created separate Factory project instead of putting all the code in Consol Application. Below is the Solution Structure.

image

I hope you all will find it useful. Thanks all for your valuable comments. Please keep it coming.

Also I am planning to start a new series related to UI with jQuery as backend. Lets see how that goes.

You can download the source code in C# from below URL:

MEF-II VS2010 NET4.zip

Managed Extensibility Framework (MEF) Example – Part II


One of the comment posted in my earlier example of MEF was what if we have two implementations of the same interface and we need to decide at run time which implementation to use.

This is where ‘ExportMetadata’ comes to the rescue. The idea here is the implementer can expose any number of metadata properties which can be anything from simple primitive data types to complex class structure. Now when we do the import we get access to these metadata which we can read and decide which implementation to use.

Lets take an example of a Car interface called ‘ICar’. This interface has two methods called ‘GetBrand’ and ‘GetWeight’. Now anyone implementing this interface will expose a metadata called ‘Type’ (e.g. SUV, Sedan etc…) which consumer can use to filter the result after importing the definition.

We will keep the project structure similar (source code uploaded along with the blog post) and also in this example we will keep the composition logic to a separate class to keep the Main function simple.

So our interface ‘ICar’ looks something like this.

image

Now lets look at the implementation part, first Impl1 (notice the ExportMetadata tag):

image

Impl2:

image

Ok, so we got our interface definition and two implementation and we said earlier in this post that we will keep our main function clean. So why not have a separate helper class which will do the composition work for us and return the result based on the criteria specified.

Now this helper class can be placed anywhere you need, but for simplicity I have put into the ConsolApp project itself. By the way the Solution structure is exactly same what we used in example one. Only thing I have added is ‘ConsolApp1’ to demonstrate the ‘ExportMetadata’ functionality.

image

Coming back to the helper class which does the composition and returns the filtered list of implementation based on the criteria specified for ‘Type’ (ExportMetadata tag specified for implementation), ,you will notice the ’CarFactory’ class in the ConsolApp1.

image

This makes our Main function in ConsolApp1 pretty simple.

image

Everything else will remain pretty much same e.g. Extension folder and copying and pasting the implementation dll’s to the extension folder. Now when we run this example we can see the result.

image

I hope you will find this information helpful and comments and suggestions are very much appreciated.

*Note: Both the examples of MEF are real time extensible where we can put the implementation dll in the Extension folder while the application is still running and we can see that the application picks up the recently pasted dll in the extension folder without restarting the application, which is very powerful.

Next we will see how we to update the implementation dll while the process is still running which will give very robust infrastructure of update of the functionality.

Stay tuned!!!

Sample Code (C#) can be downloaded from:
MEF-II.zip