List local windows groups with Forms c#

Asked

Viewed 105 times

-1

Good users!

I’m trying to list all the local windows groups with Forms c#, but I was not successful.

I want to list the groups and from there select and include in the user’s creation.

thank you in advance.

  • Can you share the code you used to try to list? It’ll be easier to help! :)

  • @Georgewurthmann I don’t really have Code assembled yet, I’m creating a little program for creating local windows users, and I wanted to find a way to list the existing groups on the computer for the person to select and when to create, already put this user in the selected groups.

  • 1

    If you don’t have the code, as I said on the topic that you’re trying but failed?

  • I actually made a user creation code, and wanted a way to pull the groups play in a checkedListBox and give the option to select and already create this user in the selected groups.

1 answer

3


For local groups maybe this is enough:

using System.DirectoryServices;

DirectoryEntry machine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer");

if (machine.Children != null)
{
    var results = machine.Children.Cast<DirectoryEntry>().Where(r => r.SchemaClassName == "Group").OrderBy(r => r.Name);

    foreach (DirectoryEntry child in results)
        Debug.Print(child.Name);
}

For something more comprehensive, for example the whole domain:

using System.DirectoryServices.AccountManagement;

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);

var results = srch.FindAll().Where(r => r.StructuralObjectClass == "group").OrderBy(r => r.Name);

// encontrar todos os resultados
foreach (var found in results)
    Debug.Print(found.Name);

Applying practising of both codes:

inserir a descrição da imagem aqui

Example 1

using System;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Linq;

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

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryEntry machine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",Computer");

            if (machine.Children != null)
            {
                var results = machine.Children.Cast<DirectoryEntry>().Where(r => r.SchemaClassName == "Group").OrderBy(r => r.Name);

                label1.Text = string.Format(@"{0:#,##0} grupos", results.Count());

                foreach (DirectoryEntry child in results)
                    checkedListBox1.Items.Add(child.Name);
            }
        }
    }
}

Example 2

using System;
using System.Windows.Forms;
using System.DirectoryServices.AccountManagement;
using System.Linq;

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

        private void button1_Click(object sender, EventArgs e)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
            PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);

            var results = srch.FindAll().Where(r => r.StructuralObjectClass == "group").OrderBy(r => r.Name);

            label1.Text = string.Format(@"{0:#,##0} grupos", results.Count());

            // encontrar todos os resultados
            foreach (var found in results)
                checkedListBox1.Items.Add(found.Name);
        }
    }
}
  • Whoa, man, blz, man! this second code I managed to get back with a Messagebox and it returned me several things, the first did not work, at home I will look calmly. my Thank you so much! you’ve already given me a light.

  • My idea is to play at a checkedListBox.

  • The first code would be the most appropriate. But what error you give in the first code?

  • actually I did not know how to do it returns something, I have no experience. ?

  • Edited response with functional examples!

  • Show! Now it worked here! @João Martins

  • Perfect! Mark the answer as correct and give an UP! :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.