public ADObject GetADObject(Guid guid) { ADObjectTypes objType; ADObject adObject; using (DirectoryEntry de = GetDirectoryEntry(guid.ToString())) { if (de != null) { var switchExpr = de.SchemaClassName; switch (switchExpr) { case SCHEMA_CLASS_BUILTINDOMAIN: { objType = ADObjectTypes.BuiltInDomain; break; } case SCHEMA_CLASS_CONTAINER: { objType = ADObjectTypes.Container; break; } case SCHEMA_CLASS_DOMAINDNS: { objType = ADObjectTypes.RootNode; break; } case SCHEMA_CLASS_GROUP: { objType = ADObjectTypes.Group; break; } case SCHEMA_CLASS_OU: { objType = ADObjectTypes.OU; break; } case SCHEMA_CLASS_USER: { objType = ADObjectTypes.User; break; } default: { objType = ADObjectTypes.Unknown; break; } } adObject = new ADObject(objType, Convert.ToString(de.Properties["name"].Value), de.Path, de.Guid); if (adObject is object) { if (de.Properties.Contains("isCriticalSystemObject") == true && (de.Properties["isCriticalSystemObject"].Value.ToString().ToLower() ?? "") == "true") { adObject.IsCriticalSystemObject = true; } if (de.Properties.Contains("showInAdvancedViewOnly") == true && (de.Properties["showInAdvancedViewOnly"].Value.ToString().ToLower() ?? "") == "true") { adObject.ShowInAdvancedViewOnly = true; } } return(adObject); } else { return(null); } } }
public List <ADObject> GetChildContainers(string parentGUID) { string rootPath, relativePath; ADObject adObject = null; var children = new List <ADObject>(); using (DirectoryEntry deRoot = GetDirectoryEntry()) { DirectoryEntry deCurrentContainer; if ((parentGUID ?? "") == (string.Empty ?? "")) { deCurrentContainer = deRoot; } else { deCurrentContainer = GetDirectoryEntry(parentGUID); } rootPath = deRoot.Path + "/"; // If deCurrentContainer.SchemaClassName = SCHEMA_CLASS_DOMAINDNS OrElse deCurrentContainer.SchemaClassName = SCHEMA_CLASS_GROUP OrElse deCurrentContainer.SchemaClassName = SCHEMA_CLASS_OU OrElse deCurrentContainer.SchemaClassName = SCHEMA_CLASS_BUILTINDOMAIN OrElse deCurrentContainer.SchemaClassName = SCHEMA_CLASS_CONTAINER Then foreach (DirectoryEntry de in deCurrentContainer.Children) { relativePath = de.Path.Replace(rootPath, ""); adObject = null; var switchExpr = de.SchemaClassName; switch (switchExpr) { case SCHEMA_CLASS_OU: { adObject = new ADObject(ADObjectTypes.OU, Convert.ToString(de.Properties["name"].Value), relativePath, de.Guid); break; } case SCHEMA_CLASS_BUILTINDOMAIN: { adObject = new ADObject(ADObjectTypes.BuiltInDomain, Convert.ToString(de.Properties["name"].Value), relativePath, de.Guid); break; } case SCHEMA_CLASS_CONTAINER: { adObject = new ADObject(ADObjectTypes.Container, Convert.ToString(de.Properties["name"].Value), relativePath, de.Guid); break; } } if (adObject is object) { if (de.Properties.Contains("isCriticalSystemObject") == true) { if ((de.Properties["isCriticalSystemObject"].Value.ToString().ToLower() ?? "") == "true") { adObject.IsCriticalSystemObject = true; } } if (de.Properties.Contains("showInAdvancedViewOnly") == true) { if ((de.Properties["showInAdvancedViewOnly"].Value.ToString().ToLower() ?? "") == "true") { adObject.ShowInAdvancedViewOnly = true; } } children.Add(adObject); } } children.Sort(new Comparer.ADObjectComparer()); // children.Reverse() return(children); } }