In this article I’m going to carry on with the follow-ups to posts I wrote in the past about various topics. Today’s topic is still on the subject of using .NET to list the users in Active Directory although this time we’re going to do it in C#. I wrote the first post and sample application using VB.NET back when I (perhaps) didn’t know as much as I know now … hopefully y’all won’t be too harsh on my n00b code from back then. 🙂
So, what’s different? Apart from the language change here is a summary of what’s changed between this version and the original.
- All references that aren’t needed have been removed.
- The results are now added to a generic list of strings (the original was just an ArrayList).
- The domain name can now be passed as a parameter on the command line. If one isn’t provided the user will be prompted.
- Regular expressions are now used to filter out user accounts that don’t match certain criteria (the original did some horribly repetitive case-sensitive matching).
- The output is now formatted better (the original was just a plain list).
- The account’s email address is now shown too.
- A total count of the accounts that can’t be ‘read’ is shown at the end.
- The app’s exception handling is a whole heap better (the original, being a sample, didn’t have any).
- The app won’t run if a domain name isn’t provided at all – an error message is displayed.
Note that this version doesn’t do any validation of the domain name e.g. length, illegal characters etc although you could use regular expressions to validate it.
On the next page is the entire source for the application – it’s a single .cs file.