Click here to Skip to main content
15,879,535 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.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;

namespace MBeatini
{
    public partial class FormOptions : Form
    {
        private HybridDictionary list = new HybridDictionary();

        public FormOptions(int op)
        {
            InitializeComponent();
            if (op == 1)
            {
                this.Text = "Add options";
                button2.Visible = false;
            }
            else
            {
                this.Text = "Remove options";
                button1.Visible = false;
            }


            list.Add("UF_TEMP_DUPLICATE_ACCOUNT", 0x0100);
            list.Add("UF_NORMAL_ACCOUNT", 0x0200);
            list.Add("UF_INTERDOMAIN_TRUST_ACCOUNT", 0x0800);
            list.Add("UF_WORKSTATION_TRUST_ACCOUNT", 0x1000);
            list.Add("UF_SERVER_TRUST_ACCOUNT", 0x2000);
            list.Add("UF_DONT_EXPIRE_PASSWD", 0x10000);
            list.Add("UF_SCRIPT", 0x0001);
            list.Add("UF_ACCOUNTDISABLE", 0x0002);
            list.Add("UF_HOMEDIR_REQUIRED", 0x0008);
            list.Add("UF_LOCKOUT", 0x0010);
            list.Add("UF_PASSWD_NOTREQD", 0x0020);
            list.Add("UF_PASSWD_CANT_CHANGE", 0x0040);
            list.Add("UF_ACCOUNT_LOCKOUT", 0x0010);
            list.Add("UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED", 0x0080);


            comboBox1.Items.Add("UF_TEMP_DUPLICATE_ACCOUNT");
            comboBox1.Items.Add("UF_NORMAL_ACCOUNT");
            comboBox1.Items.Add("UF_INTERDOMAIN_TRUST_ACCOUNT");
            comboBox1.Items.Add("UF_WORKSTATION_TRUST_ACCOUNT");
            comboBox1.Items.Add("UF_SERVER_TRUST_ACCOUNT");
            comboBox1.Items.Add("UF_DONT_EXPIRE_PASSWD");
            comboBox1.Items.Add("UF_SCRIPT");
            comboBox1.Items.Add("UF_ACCOUNTDISABLE");
            comboBox1.Items.Add("UF_HOMEDIR_REQUIRED");
            comboBox1.Items.Add("UF_LOCKOUT");
            comboBox1.Items.Add("UF_PASSWD_NOTREQD");
            comboBox1.Items.Add("UF_PASSWD_CANT_CHANGE");
            comboBox1.Items.Add("UF_ACCOUNT_LOCKOUT");
            comboBox1.Items.Add("UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED");

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // remove
            if (textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("Username: mandatory field", "Warning", MessageBoxButtons.OK);
                textBox1.Focus();
            }
            else if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Option flag: mandatory field", "Warning", MessageBoxButtons.OK);
                comboBox1.Focus();
            }
            else
            {
                UserManager um = new UserManager();
                if (!um.RemoveOptionFlagToUser(textBox1.Text, Convert.ToInt32(list[comboBox1.SelectedItem.ToString()].ToString())))
                    MessageBox.Show(um.ErrorMessage, "Warning", MessageBoxButtons.OK);
                else
                    MessageBox.Show("Option " + comboBox1.SelectedItem.ToString() + " removed.", "Warning", MessageBoxButtons.OK);

                um = null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // add
            if (textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("Username: mandatory field", "Warning", MessageBoxButtons.OK);
                textBox1.Focus();
            }
            else if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("Option flag: mandatory field", "Warning", MessageBoxButtons.OK);
                comboBox1.Focus();
            }
            else
            {
                UserManager um = new UserManager();
                if (!um.AddOptionFlagToUser(textBox1.Text, Convert.ToInt32(list[comboBox1.SelectedItem.ToString()].ToString())))
                    MessageBox.Show(um.ErrorMessage, "Warning", MessageBoxButtons.OK);
                else
                    MessageBox.Show("Option " + comboBox1.SelectedItem.ToString() + " added.", "Warning", MessageBoxButtons.OK);
                um = null;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }
    }
}

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