I'm trying to create an Organizational Unit for each OU in an LDAP path if the OU doesn't exist, the program doesn't know the OU names or how deep the OUs go, there could be 1 OU in the path or 10 deep.
Example: strPath = "OU=Test1,OU=Test2,OU=Test3,DC=Internal,DC=net"
The code below extracts the last OU 'OU=Test1' from the strPath and creates the OU, the big problem I have is that what if Test2 and Test3 also do not exist. I need to create the parent OUs first. Has anyone any suggestions on how I can tackle this?
DirectoryEntry parent;
String strOU = strPath.Substring(0, strPath.IndexOf(@","));
objOU = parent.Children.Add(strOU, "OrganizationalUnit");
objOU.CommitChanges();
I have tried using the split method with an array but I just end up with each OU created in the root and not nested OUs. The problem is the last OU in the path (Test3 above) needs creating first. I also need to keep in mind that Test3 may exist!