Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DirectoryEntry parent;
- String strOU = strPath.Substring(0, strPath.IndexOf(@","));
- objOU = parent.Children.Add(strOU, "OrganizationalUnit");
- objOU.CommitChanges();
- string strPath = "OU=Test1,OU=Test2,OU=Test3,DC=Internal,DC=net";
- // get just DC portion of distinguished name
- int dcIndex = strPath.IndexOf("DC=");
- string dcSubString = strPath.Substring(dcIndex);
- // get just OU portion of distinguished name
- string ouSubString = strPath.Substring(0, dcIndex -1);
- string tempDistinguishedName = dcSubString;
- string[] ouSubStrings = ouSubString.Split(',');
- for (int i = ouSubStrings.Length - 1; i >= 0; i--)
- {
- // bind
- DirectoryEntry parentEntry = new DirectoryEntry(tempDistinguishedName);
- // Create OU
- DirectoryEntry newOU = parentEntry.Children.Add(ouSubStrings[i], "OrganizationalUnit");
- newOU.CommitChanges();
- // create distinguishedName for next bind
- tempDistinguishedName = ouSubStrings[i] + "," + tempDistinguishedName;
- // clean up unmanaged resources
- newOU.Dispose();
- parentEntry.Dispose();
- }
Add Comment
Please, Sign In to add comment