Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

UserManager: a class to manipulate local Windows users and groups

Rate me:
Please Sign up or sign in to vote.
4.71/5 (12 votes)
2 Aug 2006CPOL 48.4K   754   40  
UserManager was built to simplify local users and groups manipulation
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace MBeatini
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.Text = "Users & Groups Browser - Computername: " + Environment.MachineName;
            Init();
        }

        private void Init()
        {
            UserManager um = new UserManager();

            ArrayList lst = new ArrayList();


            lst = um.ListGroupsInServer();
            TreeNode root = new TreeNode();
            root = treeView1.Nodes[0];

            root.Nodes.Clear();
            
            foreach (object o in lst)
            {
                TreeNode node = new TreeNode(o.ToString());
                node.ImageIndex = 0;
                node.SelectedImageIndex = 0;
                root.Nodes.Add(node);
            }

            lst.Clear();

            lst = um.ListUsersInServer();
            root = treeView1.Nodes[1];

            root.Nodes.Clear();

            foreach (object o in lst)
            {
                TreeNode node = new TreeNode(o.ToString());
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                root.Nodes.Add(node);
            }

            lst.Clear();
 

            um = null;
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if ((e.Node.Parent != null) && (e.Button == MouseButtons.Left))
            {
                UserManager um = new UserManager();
                ArrayList lst = new ArrayList();

                textBox1.Text = "";

                if (e.Node.Parent.Tag.ToString() == "user_root")
                {
                    textBox1.Text += "User: " + e.Node.Text + "\r\n\r\n" + "Properties:\r\n";
                    lst = um.UserProperties(e.Node.Text);
                    foreach (object o in lst)
                    {
                        textBox1.Text += o.ToString() + "\r\n";
                        if (o.ToString().IndexOf("UserFlags") >= 0)
                        {
                            int val = Convert.ToInt32(o.ToString().Substring(o.ToString().IndexOf('=')+1) );

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_TEMP_DUPLICATE_ACCOUNT: ";
                            if (((int)UserManager.ADAccountOptions.UF_TEMP_DUPLICATE_ACCOUNT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_NORMAL_ACCOUNT: ";
                            if (((int)UserManager.ADAccountOptions.UF_NORMAL_ACCOUNT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_INTERDOMAIN_TRUST_ACCOUNT: ";
                            if (((int)UserManager.ADAccountOptions.UF_INTERDOMAIN_TRUST_ACCOUNT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_WORKSTATION_TRUST_ACCOUNT: ";
                            if (((int)UserManager.ADAccountOptions.UF_WORKSTATION_TRUST_ACCOUNT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";


                            textBox1.Text += "\t";
                            textBox1.Text += "UF_SERVER_TRUST_ACCOUNT: ";
                            if (((int)UserManager.ADAccountOptions.UF_SERVER_TRUST_ACCOUNT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_DONT_EXPIRE_PASSWD: ";
                            if (((int)UserManager.ADAccountOptions.UF_DONT_EXPIRE_PASSWD | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_SCRIPT: ";
                            if (((int)UserManager.ADAccountOptions.UF_SCRIPT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_ACCOUNTDISABLE: ";
                            if (((int)UserManager.ADAccountOptions.UF_ACCOUNTDISABLE | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_HOMEDIR_REQUIRED: ";
                            if (((int)UserManager.ADAccountOptions.UF_HOMEDIR_REQUIRED | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_LOCKOUT: ";
                            if (((int)UserManager.ADAccountOptions.UF_LOCKOUT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_PASSWD_NOTREQD: ";
                            if (((int)UserManager.ADAccountOptions.UF_PASSWD_NOTREQD | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_PASSWD_CANT_CHANGE: ";
                            if (((int)UserManager.ADAccountOptions.UF_PASSWD_CANT_CHANGE | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_ACCOUNT_LOCKOUT: ";
                            if (((int)UserManager.ADAccountOptions.UF_ACCOUNT_LOCKOUT | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\t";
                            textBox1.Text += "UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED: ";
                            if (((int)UserManager.ADAccountOptions.UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED | val) == val)
                                textBox1.Text += "checked";
                            else
                                textBox1.Text += "unchecked";
                            textBox1.Text += "\r\n";

                            textBox1.Text += "\r\n";

                         }
                    }

                }

                if (e.Node.Parent.Tag.ToString() == "group_root")
                {
                    lst = um.GroupProperties(e.Node.Text);
                    textBox1.Text = "Group: " + e.Node.Text + "\r\n\r\n" + "Properties:\r\n";
                    foreach (object o in lst)
                        textBox1.Text += o.ToString() + "\r\n";

                    lst = um.ListUsersInGroup(e.Node.Text);
                    textBox1.Text += "\r\nMembers:\r\n";
                    foreach (object o in lst)
                        textBox1.Text += o.ToString() + "\r\n";
                }
                um = null;
            }
        }

        #region menu events

        private void newUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddUser frm = new FormAddUser();
            frm.ShowDialog();
            frm = null;
        }


        private void addOptionFlagToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormOptions frm = new FormOptions(1);
            frm.ShowDialog();
            frm = null;

        }

        private void removeOptionFlagToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormOptions frm = new FormOptions(2);
            frm.ShowDialog();
            frm = null;

        }
 
        private void newGroupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddGroup frm = new FormAddGroup();
            frm.ShowDialog();
            frm = null;
        }


        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Init();
        }
        #endregion


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions