导航

  1. 常用CHM
  2. 订阅
« 获得当前用户的Application Data文件夹位置用C#在本地创建一个Windows帐户(DOS命令) »

How to add a user to the local system by using dir

分类: C#|DOTNET 发布: yaosansi 浏览: 日期: 2006年10月30日

This article was previously published under Q306273
On This Page
SUMMARYSUMMARY
RequirementsRequirements
 
Create the SampleCreate the Sample
 
Code ExplanationCode Explanation
Create a New Directory EntryCreate a New Directory Entry
 
Add the Directory Entry to the Directory TreeAdd the Directory Entry to the Directory Tree
 
Set the Password and Description for the New User AccountSet the Password and Description for the New User Account
 
Add the Account to a GroupAdd the Account to a Group
 
TroubleshootingTroubleshooting
 



SUMMARY


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


 

Requirements


Microsoft Windows XP, Windows 2000, or Windows NT 4.0
Visual C# .NET or Visual C# 2005
 

Create the Sample


1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, 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 Class1.cs with the following code.

Note In Microsoft Visual C# 2005, Class1.cs is changed to Program.cs.
using System;
using System.DirectoryServices;
class Class1
{
static void Main(string[] args)
{
try
{
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"});
NewUser.CommitChanges();
DirectoryEntry grp;
grp = AD.Children.Find("Guests", "group");
if (grp != null) {grp.Invoke("Add", new object[] {NewUser.Path.ToString()});}
Console.WriteLine("Account Created Successfully");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
5. Compile and then run the project.
6. Follow these steps on a Windows 2000-based computer to verify that the account was created and added to the Guest group:


a. From the Start menu, point to Programs, point to Administrative Tools, and then click Computer Management.
b. Click to expand the Local Users and Groups node. The new account should appear under the Users node, as well as under the node for the Guest group.
Follow these steps on a Windows XP-based computer to verify that the account was created and added to the Guest group:
a. From the Start menu, click Control Panel.
b. Double-click User Accounts. The new user account should appear in the User Accounts dialog box.
IMPORTANT NOTE: Remove the newly created user account from the system after you finish testing.
 

Code Explanation


Create a New Directory Entry


When you create the directory entry in this sample, it is assumed that the system is running Microsoft Windows NT, Windows 2000, or Windows XP. Note that the string that is passed to the DirectoryEntry constructor begins with "WinNT://". You can also run Directory Services on other third-party operating systems.
DirectoryEntry AD = new DirectoryEntry("WinNT://" + SystemInformation.ComputerName + ",computer");

Add the Directory Entry to the Directory Tree


The following code adds a DirectoryEntry of type user with the value of TestUser1 to the Active Directory tree.
DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");

Set the Password and Description for the New User Account


The following code calls the Invoke method to invoke the SetPassword and Put methods of the DirectoryEntry object. This sets the password and assigns a description to the user account. This code also calls the CommitChanges method to save the changes.
NewUser.Invoke("SetPassword", new object[] {"#12345Abc"});
NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"});
NewUser.CommitChanges();

Add the Account to a Group


To add the account to a group, follow these steps:
1. Define a variable of type DirectoryEntry.
2. Call the Find method of the Children member of the ActiveDirectory class to populate the variable. In this case, the Guest group is the target of the search. This code tests the value that the Find method returns to determine if the group has been found. If the group is found, the new user account is added to the group.

DirectoryEntry grp;
grp = AD.Children.Find("Guests", "group");
if (grp != null) {grp.Invoke("Add", new object[] {NewUser.Path.ToString()});}

 

Troubleshooting


The code in this article fails if you try to run the code without the sufficient privileges to create a user account. For the code to complete successfully, the currently logged on user must be a member of the Administrators group or have specific permissions that allow the user to create user accounts.


 


APPLIES TO
Microsoft Visual C# 2005
Microsoft Visual C# .NET 2002 Standard Edition
 
Keywords: 
kbhowtomaster KB306273

相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Walle Build 91204

Copyright 2005-2010 yaosansi'site All Rights Reserved.
感谢系统大玩家为本站提供FTP空间
辽ICP备05021434号