views:

561

answers:

5

Hi,

I am doing an C# application targeting WinXP, Vista, and 7 Operating Systems.

One feature is, I can Add, Remove, Modify the Group set to a user programmatically.

Can I ask for help how to make this happen?

Will it be possible to do this in WMI? My codes mainly using WMI to get the users..

A: 

If your using Active Directory this might help.

http://www.codeproject.com/KB/system/everythingInAD.aspx

JeremySpouken
Will LDAP works with my targeted operating systems? Wasn't that for Servers only?
Nullstr1ng
+1  A: 

If you're using local groups, you can do this by calling the system net command. For example, to add a user to a group, you'd call:

net localgroup MyGroup /add SomeUser

Type net help localgroup at a command prompt for more info.

You can also do this using WMI. This is VBScript but can be adapted to .NET or your preferred programming toolkit:

Dim oComputer
Computer = "computername"
Groupname = "Administrators"
ObjectToAdd = "Administrator"

' Bind to the computer.
Set oComputer = GetObject("WinNT://" & Computer & ",computer")

' get group object
Dim oGroup
Set oGroup = oComputer.GetObject("group", GroupName)

' Add the user object to the group.
oGroup.Add "WinNT://" & Computer & "/" & ObjectToAdd 

Credit: Matt Hickman, http://www.tech-archive.net/Archive/WinXP/microsoft.public.windowsxp.wmi/2004-04/0007.html

itowlson
YES PLEASE WMI!!
Nullstr1ng
Will adding a User to a group possible in WMI?Is there like Update query? :D
Nullstr1ng
+1  A: 

etc etc

Remus Rusanu
A: 

Currently am using Windows7

Am trying to test this code

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer");
localMachine.Properties["member"].Add("Chevi");
localMachine.CommitChanges();
localMachine.Close();

and it's spitting this error

"The directory property cannot be found in the cache."

I tried enumerating the Property collection and I got this OperatingSystem

OperatingSystemVersion

Owner

Division

ProcessorCount

Processor

Name

Nullstr1ng
A: 

I got it working

NEW QUESTION
Is it ok to enumerate the members of a group to know if a user exists or there's another way?

Nullstr1ng