473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Detect if a local user/group account exists

Hi,

Given a name, I want to be able to detect if a user or a group account
exists with that name on a system and know if its a user or a group. My
search yielded the "LookupAccountName" Win32 API but I haven't found
any class in .NET that allows me to do that.

Does any one know of a class in .NET or a way of doing this without
using Win32 APIs ?

Thanks,
Roshan Achar

Apr 25 '06 #1
5 4047
Hello,

I think, a quick hack would be this:

public bool GroupExists(string name) {
try
{
NTAccount account = new NTAccount(name);
acc.Translate(typeof(SecurityIdentifier));
return true;
}
catch (PrincipalNotMappedException)
{
return false;
}
}

Not very elegant, though.

Best regards,
Henning Krause

"Roshan" <br*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi,

Given a name, I want to be able to detect if a user or a group account
exists with that name on a system and know if its a user or a group. My
search yielded the "LookupAccountName" Win32 API but I haven't found
any class in .NET that allows me to do that.

Does any one know of a class in .NET or a way of doing this without
using Win32 APIs ?

Thanks,
Roshan Achar

Apr 25 '06 #2
Thanks Henning.

This will suffice for the moment. It would have been better if an API
existed to do this directly.Also the Exception thrown is
IdentityNotMappedException
I found that I can detect if the name is a user or a group using

SecurityIdentifier.IsAccountSid(). It returns true if the name is a
local user account and false if its a group.

Thanks,
Roshan

Henning Krause [MVP] wrote:
Hello,

I think, a quick hack would be this:

public bool GroupExists(string name) {
try
{
NTAccount account = new NTAccount(name);
acc.Translate(typeof(SecurityIdentifier));
return true;
}
catch (PrincipalNotMappedException)
{
return false;
}
}

Not very elegant, though.

Best regards,
Henning Krause

"Roshan" <br*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi,

Given a name, I want to be able to detect if a user or a group account
exists with that name on a system and know if its a user or a group. My
search yielded the "LookupAccountName" Win32 API but I haven't found
any class in .NET that allows me to do that.

Does any one know of a class in .NET or a way of doing this without
using Win32 APIs ?

Thanks,
Roshan Achar


Apr 26 '06 #3
Hi Henning,

I've been looking for this for a long time. But it solves just one problem
for me.
I want to test if a group exists, and if not, I want to create it.

How can i do this?

Any idea?
"Henning Krause [MVP]" <ne***************@this.infinitec.de> schrieb im
Newsbeitrag news:uA**************@TK2MSFTNGP04.phx.gbl...
Hello,

I think, a quick hack would be this:

public bool GroupExists(string name) {
try
{
NTAccount account = new NTAccount(name);
acc.Translate(typeof(SecurityIdentifier));
return true;
}
catch (PrincipalNotMappedException)
{
return false;
}
}

Not very elegant, though.

Best regards,
Henning Krause

"Roshan" <br*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi,

Given a name, I want to be able to detect if a user or a group account
exists with that name on a system and know if its a user or a group. My
search yielded the "LookupAccountName" Win32 API but I haven't found
any class in .NET that allows me to do that.

Does any one know of a class in .NET or a way of doing this without
using Win32 APIs ?

Thanks,
Roshan Achar


May 25 '06 #4
Hi Knut,

You can create a group as follows

using (DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer"))
{
try
{
String typeName = "group";
String accountName = "KnutGroup";
using (DirectoryEntry NewUser =
AD.Children.Add(accountName, typeName))
{
NewUser.CommitChanges();
}
}
catch
{
Console.WriteLine("Error occured");
}
}

Hope this suffices,

Thanks,
Roshan

Knut Wehrle wrote:
Hi Henning,

I've been looking for this for a long time. But it solves just one problem
for me.
I want to test if a group exists, and if not, I want to create it.

How can i do this?

Any idea?
"Henning Krause [MVP]" <ne***************@this.infinitec.de> schrieb im
Newsbeitrag news:uA**************@TK2MSFTNGP04.phx.gbl...
Hello,

I think, a quick hack would be this:

public bool GroupExists(string name) {
try
{
NTAccount account = new NTAccount(name);
acc.Translate(typeof(SecurityIdentifier));
return true;
}
catch (PrincipalNotMappedException)
{
return false;
}
}

Not very elegant, though.

Best regards,
Henning Krause

"Roshan" <br*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi,

Given a name, I want to be able to detect if a user or a group account
exists with that name on a system and know if its a user or a group. My
search yielded the "LookupAccountName" Win32 API but I haven't found
any class in .NET that allows me to do that.

Does any one know of a class in .NET or a way of doing this without
using Win32 APIs ?

Thanks,
Roshan Achar



Jun 16 '06 #5
Hi Roshan,

that's great!!! Thank you!
I've been looking for the solution very long.

Best Regards
Knut

"Roshan" <br*****@gmail.comschrieb im Newsbeitrag
news:11**********************@r2g2000cwb.googlegro ups.com...
Hi Knut,

You can create a group as follows

using (DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer"))
{
try
{
String typeName = "group";
String accountName = "KnutGroup";
using (DirectoryEntry NewUser =
AD.Children.Add(accountName, typeName))
{
NewUser.CommitChanges();
}
}
catch
{
Console.WriteLine("Error occured");
}
}

Hope this suffices,

Thanks,
Roshan

Knut Wehrle wrote:
>Hi Henning,

I've been looking for this for a long time. But it solves just one
problem
for me.
I want to test if a group exists, and if not, I want to create it.

How can i do this?

Any idea?
"Henning Krause [MVP]" <ne***************@this.infinitec.deschrieb im
Newsbeitrag news:uA**************@TK2MSFTNGP04.phx.gbl...
Hello,

I think, a quick hack would be this:

public bool GroupExists(string name) {
try
{
NTAccount account = new NTAccount(name);
acc.Translate(typeof(SecurityIdentifier));
return true;
}
catch (PrincipalNotMappedException)
{
return false;
}
}

Not very elegant, though.

Best regards,
Henning Krause

"Roshan" <br*****@gmail.comwrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi,

Given a name, I want to be able to detect if a user or a group account
exists with that name on a system and know if its a user or a group.
My
search yielded the "LookupAccountName" Win32 API but I haven't found
any class in .NET that allows me to do that.

Does any one know of a class in .NET or a way of doing this without
using Win32 APIs ?

Thanks,
Roshan Achar

Jul 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
6
by: Rob | last post by:
Hi, I am working on a project that requires a Windows Service which performs the following file transfer functions. 1. It monitors a specific local directory on a Windows 2003 Server. 2. When...
1
by: Next | last post by:
I have a new domain account to use with my Windows Services. The account name is WinServ. I am trying to test my Windows Service on my XP Pro machine. However the service won't start unless...
16
by: JonnyD | last post by:
I am working on a project to build a reporting web app from an exsiting database that is controled by a local application. The application that has control over the database creates a lock file to...
5
by: Rocky | last post by:
Hi, I have a webform, with 2 textboxs and a submit button. In the text box1, i enter a username and in textbox2 I enter the computer name. Both the username and computer name is in active...
0
by: robpimentel | last post by:
Hi, I've been using DB2 for about 1 week, so please bear with me. DB2 Connect Enterprise Edition v8.1 FixPack 5 Windows Server 2003 Standard Edition SP1 Here is an error that continues to...
0
by: Chad Dressler | last post by:
I'm trying to use System.DirectoryServices to add a domain user to a local group via a web application which runs under the identity of a local admin account. Is this possible? I can...
5
by: Michael Howes | last post by:
I'm writing a utility to manage a machines *local* accounts in c# I am getting all the users in a specific Group just fine but when I want to get some of the information on each user from their...
8
by: Michael Howes | last post by:
I have some code that manages local user logins. When I create a new user I want to set the password to expire every x days and the number of failed login attempts before the account is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.