// Gets the physical path of a directory entry type public static string GetPath(DirectoryEntryRootType rootType, string domain) { string path; switch (rootType) { case DirectoryEntryRootType.AppPool: path = "IIS://" + domain + "/W3SVC/AppPools"; break; case DirectoryEntryRootType.IIS: path = "IIS://" + domain + "/W3SVC/1/ROOT"; break; default: return(null); } return(path); }
// Gets the root of a directory entry type public static DirectoryEntry GetRoot(DirectoryEntryRootType rootType, string domain, string username, string password) { string path = GetPath(rootType, domain); DirectoryEntry root; if (!string.IsNullOrEmpty(username)) { root = new DirectoryEntry(path, username, password); } else { root = new DirectoryEntry(path); } if (root == null) { throw new Exception("Could not get the directory entry. Please check if you have enough access rights to complete this operation"); } return(root); }