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

  • SEARCH
  • Home
  • 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 453347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:09:45+00:00 2026-05-12T22:09:45+00:00

I have an application that checks to see if a user exists (if not

  • 0

I have an application that checks to see if a user exists (if not create it) every time it starts. This is done as follows:

bool bUserExists = false;
DirectoryEntry dirEntryLocalMachine = 
    new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");

DirectoryEntries dirEntries = dirEntryLocalMachine.Children;

foreach (DirectoryEntry dirEntryUser in dirEntries)
{
    bUserExists = dirEntryUser.Name.Equals("UserName", 
        StringComparison.CurrentCultureIgnoreCase);

    if (bUserExists)
      break;
}

The problem is on the majority of the systems where it is deployed. This can take 6 – 10 seconds, which is too long … I need to find a way to reduce this (as much as possible). Is there a better or faster way I can use to verify if a user exists on the system or not?

I know there are other ways to solve this, like have the other applications sleep for 10 seconds, or have this tool send a message when it is ready, etc… But if I can greatly reduce the time it takes to find the user, it would make my life much easier.

  • 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-12T22:09:46+00:00Added an answer on May 12, 2026 at 10:09 pm

    .NET 3.5 supports new AD querying classes under the System.DirectoryServices.AccountManagement namespace.

    To make use of it, you’ll need to add “System.DirectoryServices.AccountManagement” as a reference AND add the using statement.

    using System.DirectoryServices.AccountManagement;
    
    
    using (PrincipalContext pc = new PrincipalContext(ContextType.Machine))
    {
        UserPrincipal up = UserPrincipal.FindByIdentity(
            pc,
            IdentityType.SamAccountName,
            "UserName");
    
        bool UserExists = (up != null);
    }
    

    < .NET 3.5

    For versions of .NET prior to 3.5, here is a clean example I found on dotnet-snippets

    DirectoryEntry dirEntryLocalMachine =
        new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
    
    bool UserExists =
        dirEntryLocalMachine.Children.Find(userIdentity, "user") != null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that needs to check a website feed every second. Sometimes
I have an application that passes in java.util.Date. I want to check whether this
I want to have functionality on my application that lets a user check off
I have application that is up more than 3 days. I can see in
I have an application that loads lots of data into memory (this is because
I am trying create a small web application that allows a user to login
Thanks for helping with this. I have a script that checks logins and then
i have read posts regarding this but that did not gave answer to my
I have a photo application where the user can create albums. My concern is
I have a Inno Setup project that I want to check if the application

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.