Expanding Active Directory (AD) Distribution Group (DG) members recursively…

In one of my projects there was a requirement to get the unique list of members from a given DG, which may have multiple levels of nested DGs.  So, I thought of using the “System.DirectoryServices” namespace with “DirectoryEntry” & “DirectorySearcher” classes.  But the challenge was to recursively expand the nested DGs and eliminate the duplicates across all nested DGs.  I planned to write a recursive method which will do this.  But, fortunately I found out that .NET Framework 3.5 introduced a new namespace “System.DirectoryServices.AccountManagement” which has more sophisticated methods to deal with AD.  I started using the GroupPrincipal.GetMembers(bool recursive) method which did the job for me.  Awesome… I just thought of sharing this as I spent quite a time in finding this solution.   HAPPY CODING!!!