views:

239

answers:

2

New to C#. I am trying to browse AD for a particular OU. I get the following error. error code 2147016646. I tried running the program with higher privl. acct. But still get the same error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
             string objectPath = "Server1";
             try
             {
                if (DirectoryEntry.Exists("LDAP://" + objectPath))
                    Console.WriteLine(objectPath + "exists");
                else
                    Console.WriteLine(objectPath + " does not exists");
            }
            catch (DirectoryServicesCOMException e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }      
    }
}
A: 

You will probably have to use something more than just "Server1" for your LDAP path.

Try something like:

string objectPath = "Server1/cn=Users,dc=yourcompany,dc=som";
try
{
   if (DirectoryEntry.Exists("LDAP://" + objectPath))
      Console.WriteLine(objectPath + "exists");
   else
      Console.WriteLine(objectPath + " does not exists");
}

This would check if the default "Users" container on your server exists (or not).

Marc

marc_s
A: 

Highly recommend "The .NET Developer's Guide To Directory Services Programming" ISBN 0-321-35017-0

Worth its weight in gold!

0xG