tags:

views:

138

answers:

1

Hi,

How to remove windows group account using C#

I'm using this code to remove user account, but I need to remove the whole group.

            DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
            DirectoryEntries users = localDirectory.Children;
            DirectoryEntry user = users.Find(userDn);
            users.Remove(user);

I need to remove the Group with it's users.

A: 
public void Delete(string ouPath, string groupPath)
{
    if (DirectoryEntry.Exists("LDAP://" + groupPath))
    {
        try
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + ouPath);
            DirectoryEntry group = new DirectoryEntry("LDAP://" + groupPath);
            entry.Children.Remove(group);
            group.CommitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }
    }
    else
    { 
        Console.WriteLine(path + " doesn't exist"); 
    }
}

Howto: (Almost) Everything In Active Directory via C#

abmv
What is ouPath??
check link..............
abmv
Thanks, I got exactly what I need it, the link I got it from your CodeProject article.http://msdn.microsoft.com/en-us/library/ms180906(VS.80).aspx