Adding Local Users to Local Groups

  • Thread starter Thread starter enilno
  • Start date Start date
E

enilno

I'm working on a C# app., and I need to programmatically add a local user to
a local group on the computer on which the app. is running. When end-users
run this app., they will be logged on as local Administrator (so they should
have sufficient permissions).

Would any one out there know how to programmatically add a local user to a
local group in C#?

TIA
 
Sorry, I guess I should note that the app. will run on either Win2k, WinXP,
or Win2003.
 
I'm working on a C# app., and I need to programmatically add a local user to
a local group on the computer on which the app. is running. When end-users
run this app., they will be logged on as local Administrator (so they should
have sufficient permissions).
Would any one out there know how to programmatically add a local user to a
local group in C#?

You can use the System.DirectoryServices namespace and the WinNT:
provider for this - check out the S.DS portal on MSDN here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/portal.asp

Basically, you need to bind to the group in question using something
like WinNT://<server>/<groupname>,group as the binding string, and
then add the user to it using the .Add method on the DirectoryEntry
object.

Marc
 
Marc,
Thanks for the reply. I tried working with your suggestions, but I'm having
some troubles. Here's my code:

string computerName = "";
computerName = System.Environment.MachineName;

string strPath = "";
strPath = "WinNT://" + computerName;

System.DirectoryServices.DirectoryEntry de = new
System.DirectoryServices.DirectoryEntry(strPath);

System.DirectoryServices.DirectoryEntry group =
de.Children.Find("RP_ADMINS");
System.DirectoryServices.DirectoryEntry usr =
de.Children.Find("Administrator");

int returnvalue = 0;
returnvalue = group.Properties["member"].Add(usr.Properties["Name"].Value);
group.CommitChanges();


I get an exception on the line where I'm calling the Add method, because
it's saying that the usr.Properties["Name"].Value argument is null.

Do you have any suggestions? Am I completely off base with this? Should I
be doing it differently?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top