tags:

views:

686

answers:

4

Hi,

How to remove windows user account using C#?

Thanks,

+1  A: 

Check out this link

cgreeno
I'm not using a domain, plus this is only removes user from group. Thanks
it may not but it gives a fairly detailed example on working with Windows Active directories.
cgreeno
However, if you don't feel it is relevant then I can remove it.
cgreeno
+2  A: 

Something like this should do the trick(not tested):

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" +  Environment.MachineName);

DirectoryEntries entries = localMachine.Children;
DirectoryEntry user = entries.Remove("User");
entries.CommitChanges();
Claus Thomsen
+6  A: 

Clause Thomsen was close, you need to pass the DirectoryEntry.Remove method a DirectoryEntry paramenter and not a string, like:

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

Alternatively using System.DirectoryServices.AccountManagement in .NET 3.5:-

http://msdn.microsoft.com/en-us/library/bb924557.aspx