如何使用C#删除Windows组帐户

时间:2009-06-15 08:22:57

标签: c# windows

如何使用C#删除Windows组帐户

我正在使用此代码删除用户帐户,但我需要删除整个群组。

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

我需要用它的用户删除该组。

1 个答案:

答案 0 :(得分:0)

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#