操作活动目录Active Directory

(MSDN 的System.DirectoryServices)
添加System.DirectoryServices.dll引用

using System;
using System.DirectoryServices;

public class ADRead {
    public static void Main(String[] args) {
        if(args.Length!=1) {
            Console.WriteLine("用法:" + Environment.GetCommandLineArgs()[0] + " <ad_path>");
            Console.WriteLine ();
            Console.WriteLine ("按 Enter 键继续...");
            Console.ReadLine();
            return;
        }

        DirectoryEntry objDirEnt=new DirectoryEntry(args[0]);
        Console.WriteLine("名称            = " + objDirEnt.Name);
        Console.WriteLine("路径            = " + objDirEnt.Path);
        Console.WriteLine("架构类别名称 = " + objDirEnt.SchemaClassName);
        Console.WriteLine("");
        Console.WriteLine("属性::");

        foreach(String Key in objDirEnt.Properties.PropertyNames) {
            Console.Write("\t{0} = ", Key);
            Console.WriteLine("");
            foreach(Object objValue in objDirEnt.Properties[Key]) {
                Console.WriteLine("\t\t{0}", objValue);
            }
        }
    }
}
 
using System;
using System.DirectoryServices;

public class ADWrite {
    public static void Main(String[] args) {
        if ( args.Length!=3 ) {
            Console.WriteLine("用法:" + Environment.GetCommandLineArgs()[0] + " <ad_path> <property> <value>");
            Console.WriteLine ();
            Console.WriteLine ("按 Enter 键继续...");
            Console.ReadLine();
            return;
        }

        DirectoryEntry objDirEnt = new DirectoryEntry(args[0]);
        Console.WriteLine("名称            = " + objDirEnt.Name);
        Console.WriteLine("路径            = " + objDirEnt.Path);
        Console.WriteLine("架构类别名称 = " + objDirEnt.SchemaClassName);
        Console.WriteLine(args[1] + " = " + (objDirEnt.Properties[args[1]][0]).ToString());
        Console.WriteLine("... 正在更改为 ");
        (objDirEnt.Properties[args[1]])[0] = args[2];
        objDirEnt.CommitChanges();
        Console.WriteLine(args[1] + " = " + (objDirEnt.Properties[args[1]][0]).ToString());
    }
}
Contributors: FHL