C# 以编程方式将本地用户添加到本地组

C# 以编程方式将本地用户添加到本地组,c#,active-directory,wmi,directoryservices,active-directory-group,C#,Active Directory,Wmi,Directoryservices,Active Directory Group,我正在做一个针对WinXP、Vista和7操作系统的C#应用程序 一个特性是,我可以通过编程方式向用户添加、删除、修改组集 我能寻求帮助吗?如何实现这一点 是否可以在WMI中执行此操作?我的代码主要使用WMI获取用户 目前我正在使用Windows7 我正在尝试测试这段代码 DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer"); localMachi

我正在做一个针对WinXP、Vista和7操作系统的C#应用程序

一个特性是,我可以通过编程方式向用户添加、删除、修改组集

我能寻求帮助吗?如何实现这一点

是否可以在WMI中执行此操作?我的代码主要使用WMI获取用户


目前我正在使用Windows7

我正在尝试测试这段代码

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

在缓存中找不到目录属性

我试着列举财产集合,我得到了这个

OperatingSystem

OperatingSystemVersion

Owner

Division

ProcessorCount

Processor

Name

如果您使用的是本地组,则可以通过调用system
net
命令来完成此操作。例如,要将用户添加到组中,您可以调用:

net localgroup MyGroup /add SomeUser
在命令提示下键入
net help localgroup
,以获取更多信息

也可以使用WMI执行此操作。这是VBScript,但可适用于.NET或您首选的编程工具包:

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 
信用证:马特·希克曼,

  • 创建用户
  • 创建组的步骤
  • 将用户添加到组的步骤
  • 从组中删除用户的步骤
  • 创建本地组的步骤
  • 将用户添加到本地组的步骤

etc etc

我还使用C#在Visual Studio 2010上开发了一个windows应用程序。这是该程序的工作版本,将向特定组添加现有用户

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;
        }
        }
    }

在WMI中将用户添加到组中是否可行?是否有类似的更新查询?:DI将使用net命令。WMI经常中断,而且速度很慢。不确定网络命令的速度,但最坏情况下,它会更可靠。