简体   繁体   English

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

[英]Programmatically add Local User to a Local Group

I am doing a C# application targeting WinXP, Vista, and 7 Operating Systems. 我正在做一个针对WinXP,Vista和7操作系统的C#应用​​程序。

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? 在WMI中可以这样做吗? My codes mainly using WMI to get the users.. 我的代码主要使用WMI来吸引用户。


Currently am using Windows7 当前正在使用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 我尝试枚举Property集合,并且得到了这个

OperatingSystem

OperatingSystemVersion

Owner

Division

ProcessorCount

Processor

Name

If you're using local groups, you can do this by calling the system net command. 如果使用本地组,则可以通过调用system net命令来执行此操作。 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. 在命令提示符下键入net help localgroup以获取更多信息。

You can also do this using WMI. 您也可以使用WMI执行此操作。 This is VBScript but can be adapted to .NET or your preferred programming toolkit: 这是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 

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

I have also developed one windows application on Visual Studio 2010, using C#. 我还使用C#在Visual Studio 2010上开发了一个Windows应用程序。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM