Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6117815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:21:50+00:00 2026-05-23T15:21:50+00:00

I want to Read, Add and Delete users from a Windows using .NET code.

  • 0

I want to Read, Add and Delete users from a Windows using .NET code. How can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:21:51+00:00Added an answer on May 23, 2026 at 3:21 pm

    Here’s some sample code for creating a windows user:

    public static bool CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires)
    {
        try
        {
            PrincipalContext context = new PrincipalContext(ContextType.Machine);
            UserPrincipal user = new UserPrincipal(context);
            user.SetPassword(password);
            user.DisplayName = displayName;
            user.Name = username;
            user.Description = description;
            user.UserCannotChangePassword = canChangePwd;
            user.PasswordNeverExpires = pwdExpires;
            user.Save();
    
            //now add user to "Users" group so it displays in Control Panel
            GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Users");
            group.Members.Add(user);
            group.Save();
    
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error creating account: {0}", ex.Message);
            return false;
        }
    
    }
    

    Adding a reference to System.DirectoryServices will let you read all windows users doing something like this:

    DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
    DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group");
    object members = admGroup.Invoke("members", null);
    foreach (object groupMember in (IEnumerable)members)
    {
        DirectoryEntry member = new DirectoryEntry(groupMember);
        lstUsers.Items.Add(member.Name);
    }
    

    The DirectoryServices Namespace in general should let you navigate and read the Active Directory

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read and write from serial using events/interrupts. Currently, I have it
I have a text file. I want read that file. But In that if
I want to read line n1->n2 from file foo.c into the current buffer. I
I want to read UTF-8 input in Perl, no matter if it comes from
I want to read each line from a text file and store them in
i have data that i fetch from an email server i want to eliminate
I want to add to my app a thin email client where you can
I have a paperclip'd file that I want to add as an attachment to
My users can change the Locale within the app (they may want to keep
i can't add 2 column in my existing db. i read any topic on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.