2

我正在做一个针对 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
4

3 回答 3

2

我还在 Visual Studio 2010 上使用 C# 开发了一个 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;
        }
        }
    }
于 2012-02-10T10:25:58.787 回答
2

如果您使用的是本地组,则可以通过调用系统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 

学分:马特·希克曼,http ://www.tech-archive.net/Archive/WinXP/microsoft.public.windowsxp.wmi/2004-04/0007.html

于 2010-03-06T02:53:34.643 回答
1

等等等等

于 2010-03-06T02:54:21.213 回答