|
||||
|
|
Wednesday, December 29, 2004Recursively List Virtual Directories in IIS with C# and DirectoryServices
You've probably seen a lot of VBScript scripts that manage IIS via ADSI objects, but let's say you want to write something in .NET. Maybe C#? Maybe compiled code, with a UI?
Enter DirectoryServices, a .NET-friendly wrapper for ADSI (Active Directory Services Interface). IIS (along with LDAP and WinNT) is known as an ADSI Provider, meaning it supports the ADSI interface, enabling you to work with files, directories, etc. So your .NET application can use DirectoryServices to create, delete, and enumerate IIS directories and virtual directories, like this: Your .NET Application <-> DirectoryServices <-> ADSI <-> IIS Below is a simple code snippet that will list out all virtual directories in an IIS web site. It's different than other scripts I've seen in that it will list virtual directories below the root. // call the function once to kick it off By default the code runs under the credentials of the account running the application, but you can specify a different account using the DirectoryEntry.Username & DirectoryEntry.Password properties, e.g. DirectoryEntry de = new DirectoryEntry("IIS://localhost/W3SVC/1");Lastly, note that in order to use System.DirectoryServices in your C# project, you'll need to deliberately add a reference to the .NET System.DirectoryServices.dll. You can research the classes more on MSDN. Archives September 2004 October 2004 November 2004 December 2004 January 2005 February 2005 March 2005 April 2005 |
|||
| ||||