Question

I am doing a 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..


Currently am using Windows7

I 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
Was it helpful?

Solution

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

OTHER TIPS

I have also developed one windows application on Visual Studio 2010, using C#. This is a working version of the program, which will add an existing user to a particular group.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace Utility_Add_User_To_Group {

    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }

        private void btn_Add_Click(object sender, EventArgs e) {
            string usr, grp;
            usr = txt_UserName.Text;
            grp = txt_GroupName.Text;

            add(usr, grp);
            groupBox2.Visible=true;
        }

        private void add(string usr, string grp) {
            bool flagUsr, flagGrp;
            try {
                DirectoryEntry AD = new DirectoryEntry("WinNT://" +Environment.MachineName + ",computer");
                DirectoryEntry group, user;

                group = AD.Children.Find(grp, "group");
                user = AD.Children.Find(usr, "user");
                if (user != null) {
                    label3.Text += "User Name Exists!!!";
                    flagUsr = true;
                } else {
                    label3.Text += "Sorry, No Such User Name Found!!!";
                    flagUsr = false;
                }

                if (group != null) {
                    label4.Text += "Group Exists!!!";
                    flagGrp = true; 
                } else {
                    label4.Text += "Sorry, Group Does Not Exists!!!";
                    flagGrp= false;
                }

                if(flagGrp == true && flagUsr == true) {
                    group.Invoke("Add", new object[] { user.Path.ToString() });
                    label5.Text += "Congratulations!!! User has been added to the group";
                } else {
                    label5.Text += "Error Happened!!! User could not be added to the group!!!";
                }
            } catch (Exception e) {
                label6.Text +=e.Message.ToString();
                label6.Visible= true;
            }
            }

        private void btn_Clear_Click(object sender, EventArgs e) {
            normal();
        }
        private void normal() {
            txt_GroupName.Text="";
            txt_UserName.Text="";
            txt_UserName.Focus();

            groupBox2.Visible=false;
        }
        }
    }

etc etc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top