Add Local User Account in C# Application

07 Oct

This step-by-step article shows you how to use the DirectoryServices namespace to add a user to the local system and a group.

  1. Start Microsoft Visual Studio .NET 2003, Microsoft Visual Studio 2005, or Microsoft Visual Studio 2008, and then create a new Visual C# Console Application project.
  2. In Solution Explorer, right-click References, and then click Add Reference.
  3. Add a reference to the System.DirectoryServices.dll assembly.
  4. Replace the code in the Class1.cs file with the following code.Note In Microsoft Visual C# 2005 or Microsoft Visual C# 2008, the Class1.cs file is replaced by the Program.cs file.
using System;
using System.DirectoryServices;

class Class1
    static void Main(string[] args)
	 DirectoryEntry AD = new DirectoryEntry("WinNT://" +
	                     Environment.MachineName + ",computer");
	 DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");
	 NewUser.Invoke("SetPassword", new object[] {"#12345Abc"});
	 NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"});
	 DirectoryEntry grp;

	 grp = AD.Children.Find("Guests", "group");
	 if (grp != null) {grp.Invoke("Add", new object[] {NewUser.Path.ToString()});}
	 Console.WriteLine("Account Created Successfully");
    catch (Exception ex)



Leave a comment

Posted by on October 7, 2009 in Window Application


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: