title: Find the domain component from a PrincipalContext
body: <p>I am trying to find out the domain component via Active Directory Services using a PrincipalContext.</p>
  
  <p>I created the PrincipalContext using very few parameters:</p>
  
  <pre><code>PrincipalContext theContext = new PrincipalContext(ContextType.Domain);
  </code></pre>
  
  <p>theContext is coming back correctly. I can query many things on it and get expected results. But what I would really like to do is the equivalent of:</p>
  
  <pre><code>Console.WriteLine("Domain Component: " + theContext.Container);
  </code></pre>
  
  <p>According to MSDN, this only "Gets the value specified in the container parameter in the constructor." Since I've passed in nothing, I'm getting nothing back.</p>
  
  <p>But the theoretical container has the domain components, which are needed for any distinguished names you might need to use or create. I'm specifically looking to create a new user in an organizational unit that I know will be there. But, since I don't know the domain components, I can't create the distinguished name. I haven't seen any support for relative paths.</p>
  
  <p>The best option I've figured is to do a search for any user and then get its distinguished name and chop off the "dc" part. </p>
  
  <pre><code>var searchUser = new UserPrincipal(theContext);
  var searcher = new PrincipalSearcher(searchUser);
  Principal aUser = searcher.FindOne();
  if (aUser != null)
  {
      string dn = aUser.DistinguishedName;
      Console.WriteLine(dn.Substring(dn.IndexOf("dc=", StringComparison.InvariantCultureIgnoreCase)));
  
  }
  </code></pre>
  
  <p>That feels like a poor hack; too much could go wrong. I'm hoping there is something better. Does anyone have an idea?</p>