As a developer-admin (or even better – admin-developer) in one person I'm really fascinated in many features which appeared in c# with .net 2.0. And, of course, I love to 'yield'! And I love internal iterators in the anonymous methods' skin!
As an example of a basic usage of these cool features I'm going to use them for a very common admin task – traversing of a directory structure.
One of our network shares is a place, where all domain users have an access and may create/delete/modify/view different files. This is a 'sack', where they can share files and restrict access to diferent objects for different groups.
This approach to sharing files leads us to different problems, which we have to face from time to time. One of them is related to the size of the drive, where all these objects live. For this reason I need a tool, which gives me some info about files, their sizes and owners.
First, let's get the owner of the object. Since I'm using .net 2.0 I have a System.Security.AccessControl namespace, where I can find many classes, which can help me.
Then we need to get to the file level to get some sec and other info. Always recursive! And, having thousends of directories and tens of thousends of files within, I don't like 'SearchOption.AllDirectories' option in the 'GetFiles()' method any more.
First approach uses anonymous methods with very basic delegate.
And somewhere in the code
Second approach utilizes external iterators in the 2.0 form – with the 'yield' keyword.
This time we are yielding the file name and don't care what it's going to be used for.
And finally in the client code
Returning control to the client code gives me lots of potential uses of the iterators. For example, I may write a very basic tool named, let's say 'ForFile', which could do specific task for every file which is visited. Tasks are easy to extract from the tool by putting them into external library and load them on-demand, while working actually on the IFileTask inside the tool. As a decorator for the iterator we may use some IFileFilter, which could narrow the search results (by file name, file size, file content, etc.)
11-24-2006 1:48 AM