using System.DirectoryServices;
using System.Runtime.InteropServices;
using System;
class ADuser
{
public static void Main()
{
try
{
string strNodeName = "TestUser";
DirectoryEntry NewUser;
//Bind and get the computer container
DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
// delete user when existing
try
{
// this throws when no such user
NewUser = AD.Children.Find(strNodeName, "User");
AD.Children.Remove(NewUser);
}
// Catch not found exception
catch(COMException cex)
{
Console.WriteLine(cex.Message);
}