views:

116

answers:

1

I have the following program to browse all virtual directories and their sub directories and files (recursively):

static void Main(string[] args)
        {
            string serverName = Environment.MachineName;
            DirectoryEntry dir = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/ROOT", @"adminusername", @"password");
            dir.AuthenticationType = AuthenticationTypes.Secure;          
            PrintChildren(dir, 0);
        }

        private static void PrintChildren(DirectoryEntry entry,int level)
        {
            foreach (DirectoryEntry child in entry.Children)
            {
                Console.WriteLine("");
                Console.WriteLine("|");
                for (int i = 0; i < level; i++)
                {
                    Console.Write("----");
                }
                Console.Write(child.Name);

                if (child.Children != null)
                {
                    PrintChildren(child,level + 1);
                }
            }
        }

Now this program does list all the virtual directories, but only in a few cases does it list the sub-directories of a virtual directories (Such directories I have observed have Anonymous access enabled in IIS).

How do I ensure that this program is able to browse all content of IIS? Are there any other security settings that can be provided/set?

A: 

I guess you need to give the caller the proper DirectoryServicesPermission
From MSDN: Code Access Security for System.DirectoryServices

Eduardo Molteni