Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Iterators for admins
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.)

Posted 11-24-2006 1:48 AM by Michal Grzegorzewski
Filed under:



Marcin Hoppe wrote re: Iterators for admins
on 11-26-2006 1:56 PM

Have you considered using PowerShell scripts instead of standalone C# apps to automate admin tasks? Since PowerShell is extensible (you can author your own commands in C#), it seems to be an exciting tool for an admin-developer type of guy.

Michal Grzegorzewski wrote re: Iterators for admins
on 11-26-2006 2:43 PM

Heh, yes, PS is very powerful tool for admins (finally!), with it's cmdlets, etc.

But remember - PS is RTW for about 2 weeks and it only gives you part of the full potential of the entire platform. For me it's a major upgrade of cmd, nothing more. For example - you are not able to create a web page with info (with simple search) about blocked files for some users. PS is for admins in their habitats, and being admin in my company means much more... and I'm used to other group of tools, coming mostly from *X environments.

Brendan Tompkins wrote re: Iterators for admins
on 11-27-2006 9:13 AM

Nice explanation of yield and welcome!

social bookmarking service wrote re: Iterators for admins
on 03-24-2013 9:28 AM

Unhifv Major thankies for the blog article.Thanks Again. Great.

Social bookmarks wrote re: Iterators for admins
on 03-24-2013 2:40 PM

HhHPZW I loved your article.Thanks Again. Awesome.

Social bookmarks wrote re: Iterators for admins
on 04-27-2013 11:30 PM

2kzhxz wow, awesome article post.Really looking forward to read more. Really Great.

camera buying guide wrote re: Iterators for admins
on 05-14-2013 7:19 AM

Ktk3aX Im obliged for the blog article.Really looking forward to read more. Great.

social bookmarks wrote re: Iterators for admins
on 07-28-2013 6:26 AM

ESCJW8 Wow, great blog post. Cool.

amazing news wrote re: Iterators for admins
on 08-04-2013 3:06 AM

E8IlMs I really like and appreciate your article post.Really thank you! Cool.

awesome linkbuilding site wrote re: Iterators for admins
on 09-30-2013 7:51 PM

vt1zlh I value the article.Much thanks again. Really Great.

link building team wrote re: Iterators for admins
on 09-30-2013 7:51 PM

U18K6Z Really appreciate you sharing this article.Much thanks again. Want more.

best link build wrote re: Iterators for admins
on 10-16-2013 10:53 AM

anCbUI I really like and appreciate your article.Really looking forward to read more. Want more.

nick crorkzz wrote re: Iterators for admins
on 04-09-2015 1:17 AM

Ik1D4F It's actually a cool and helpful piece of information. I am glad that you shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

Add a Comment

Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)