Csharp/C Sharp by API/System.DirectoryServices/DirectoryEntry

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

DirectoryEntry.Children

  

using System;
using System.DirectoryServices;
public class MainClass
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyDirectoryObject = new DirectoryEntry();
    
    MyDirectoryObject.Path = "LDAP://HMSRevenge/rootDSE";
    MyDirectoryObject.Username = @"AAA\BBB";
    MyDirectoryObject.Password = @"MyPassword";
    MyDirectoryObject.UsePropertyCache = false;
    DirectoryEntries MyChildObjects = MyDirectoryObject.Children;
        
    PropertyCollection MyAttributes = MyDirectoryObject.Properties;
    foreach(string MyAttributeName in MyAttributes.PropertyNames)
    {
      PropertyValueCollection MyAttributeValues = MyAttributes[MyAttributeName];
      foreach(string MyValue in MyAttributeValues)
      {
        Console.WriteLine(MyAttributeName + " = " + MyValue);
      }
    }
    return 0;
  }
}


DirectoryEntry.Close()

  
using System;
using System.DirectoryServices;
class MainClass
{
   public static void Main()
   {
      DirectoryEntry de2 = new DirectoryEntry("LDAP://192.168.1.100/ou=auditing, dc=accounting, dc=ispnet1, dc=net");
      string newpath = de2.Path;
      Console.WriteLine("new path: {0}", newpath);
      de2.Close();
   }
}


DirectoryEntry.CommitChanges()

 
using System;
using System.DirectoryServices;
class MainClass
{
   public static void Main()
   {
      DirectoryEntry de = new DirectoryEntry(
        "LDAP://192.168.1.100/ou=accounting, dc=ispnet1, dc=net",
         "cn=Administrator, dc=ispnet1, dc=net", "password",
         AuthenticationTypes.ServerBind);
      DirectoryEntries children = de.Children;
      try
      {
         DirectoryEntry t = children.Find("ou=auditing");
         children.Remove(t);
         de.rumitChanges();
         Console.WriteLine("removed");
      } catch (Exception)
      {
         Console.WriteLine("the object was not found");
      }
   }
}


DirectoryEntry.DeleteTree()

 

using System;
using System.DirectoryServices;
public class DeleteObject
{
   public static void Main()
   {
      DirectoryEntry de = new DirectoryEntry(
        "LDAP://192.168.1.100/ou=accounting, dc=ispnet1, dc=net",
        "cn=Administrator, dc=ispnet1, dc=net", "password",
        AuthenticationTypes.ServerBind);
      DirectoryEntries children = de.Children;
      try
      {
         DirectoryEntry badObject = children.Find("ou=auditing");
         badObject.DeleteTree();
         de.rumitChanges();
         Console.WriteLine("the object has been deleted");
      } catch (Exception e)
      {
         Console.WriteLine("the object was not found or deleted:");
         Console.WriteLine(e.ToString());
      }
   }
}


DirectoryEntry.Password

  
using System;
using System.DirectoryServices;
public class DirectoryBinding
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyDirectoryObject = new DirectoryEntry();
    
    MyDirectoryObject.Path = "LDAP://HMSRevenge/rootDSE";
    MyDirectoryObject.Username = @"Test\yourname";
    MyDirectoryObject.Password = @"MyPassword";
    return 0;
  }
}


DirectoryEntry.Path

  
using System;
using System.DirectoryServices;
public class DirectoryBinding
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyDirectoryObject = new DirectoryEntry();
    
    MyDirectoryObject.Path = "LDAP://HMSRevenge/rootDSE";
    MyDirectoryObject.Username = @"Test\yourname";
    MyDirectoryObject.Password = @"MyPassword";
    return 0;
  }
}


DirectoryEntry.Properties

  
using System;
using System.DirectoryServices;
class MainClass
{
   public static void Main()
   {
      DirectoryEntry de = new DirectoryEntry(
         "LDAP://192.168.1.100/cn=kblum, ou=sales, dc=ispnet1, dc=net");
      Console.WriteLine("object: {0}", de.Path);
      PropertyCollection pc = de.Properties;
      foreach(string propName in pc.PropertyNames)
      {
         foreach(object value in de.Properties[propName])
            Console.WriteLine("  property = {0}   value = {1}",
               propName, value);
      }
   }
}


DirectoryEntry.Rename(String newName)

  

using System;
using System.DirectoryServices;
class MainClass
{
   public static void Main()
   {
      DirectoryEntry de = new DirectoryEntry(
        "LDAP://192.168.1.100/ou=auditing, ou=accounting, dc=ispnet1, dc=net",
        "cn=Administrator, dc=ispnet1, dc=net", "password",
        AuthenticationTypes.ServerBind);
      DirectoryEntries des = de.Children;
      DirectoryEntry badObject = des.Find("cn=test");
      badObject.Rename("cn=newName");
      de.rumitChanges();
      de.Close();
   }
}


DirectoryEntry.Username

  
using System;
using System.DirectoryServices;
public class DirectoryBinding
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyDirectoryObject = new DirectoryEntry();
    
    MyDirectoryObject.Path = "LDAP://HMSRevenge/rootDSE";
    MyDirectoryObject.Username = @"Test\yourname";
    MyDirectoryObject.Password = @"MyPassword";
    return 0;
  }
}


new DirectoryEntry()

  
using System;
using System.DirectoryServices;

public class MainClass
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyDirectoryObject = new DirectoryEntry();
    
    MyDirectoryObject.Path = "LDAP://HMSRevenge/rootDSE";
    MyDirectoryObject.Username = @"userName\AAAA";
    MyDirectoryObject.Password = @"MyPassword";
    DirectoryEntries MyChildObjects = MyDirectoryObject.Children;
    return 0;  
  }
}


new DirectoryEntry(String connection)

  
using System;
using System.DirectoryServices;
class MainClass 
{
   public static void Main()
   {
      DirectoryEntry de = new DirectoryEntry("LDAP://192.168.1.100/dc=ispnet1, dc=net");
      string ldappath = de.Path;
      Console.WriteLine("The LDAP path is: {0}", ldappath);
      de.Close();
   }
}