Check Windows Account Existence?

I have a user name, how can I check if this account exists? Do I use WMI or there is other ways to achieve it
Thanks
[126 byte] By [stardv] at [2007-11-19 14:25:19]
# 1 Re: Check Windows Account Existence?
I have a user name, how can I check if this account exists? Do I use WMI or there is other ways to achieve it

Thanks
If you are using Active Directory (which I suppose you will be), then you can use System.DirectoryServices DLL to check the existence of a particular username. Here is a snippet from MSDN.System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://Path");
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
mySearcher.Filter = ("(anr= John)");
foreach(System.DirectoryServices.SearchResult result in
mySearcher.FindAll())
{
Console.WriteLine( result.GetDirectoryEntry().Path );
}You need to add a reference to System.DirectoryServices.DLL before you can use this code.
Shuja Ali at 2007-11-9 1:52:59 >
# 2 Re: Check Windows Account Existence?
Thanks A LOT
stardv at 2007-11-9 1:54:00 >
# 3 Re: Check Windows Account Existence?
Shuja Ali,

I just need to check windows local account existance. The client computers are not in any domain. Will this example work in htis case?

Tahnks
stardv at 2007-11-9 1:54:59 >
# 4 Re: Check Windows Account Existence?
Shuja Ali,

I just need to check windows local account existance. The client computers are not in any domain. Will this example work in htis case?

Tahnks
The code I have posted above will only check for Useraccounts in a domian. I haven't tried this to check the local account though. I will see if there is anything that can be done and if i find something i'll post it here.
Shuja Ali at 2007-11-9 1:56:04 >
# 5 Re: Check Windows Account Existence?
DirectorySearcher works for LDAP provider,for WinNT you can consider WMI,or you can use DirectoryEntry though
private bool IsGuestExist()
{
try
{
DirectoryEntry de = new DirectoryEntry(
"WinNT://<computer name>/Guest",
null,
null,
AuthenticationTypes.Secure
);

return de.NativeObject!=null;
}
catch(Exception)
{
return false;
}
}
mehdi62b at 2007-11-9 1:57:05 >
# 6 Re: Check Windows Account Existence?
Thanks I just used WMI, it did a job
stardv at 2007-11-9 1:57:59 >