306273 HOW TO: Add users to local systems using directory services and Visual C # .NET (from MKBA)

xiaoxiao2021-03-06  73

The release number of this article has been CHS306273

For Microsoft Visual Basic .NET versions of this article, see

306271.

This task content

Summary requirements Creating sample code Description Troubleshooting

Summary This article describes how to use

DirectoryServices namespace Add users to the local system and group.

Back to top

Claim

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

Back to top

Create an example

Open Microsoft Visual Studio .NET and create a Visual C # Console application project. In the Solution Explorer, right-click the reference, and then click Add Reference. Add a reference to the System.DirectoryServices.dll assembly. Replace the code in the class1.cs to the following code: use 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 ();

}

}

} Compile and run the project. In the following steps on Windows 2000-based computers, verify that the account has been created and added to the guest group:

From the start menu, point to the program, then point to Administrative Tools, and then click Computer Management. Click Local User and Group Nodes to expand. The new account will be displayed under the User Node and will appear under the node of the guest group. To follow the following steps on a Windows XP-based computer to verify that the account has been created and added to the guest group:

From the Start menu, click Control Panel. Double-click the user account. The new user account will be displayed in the User Account dialog box. Importantly, you should remove the newly created user account from the system after testing.

Back to top

Code description

Creating a new directory entry When you create a directory item in this example, assume that the system is running Microsoft Windows NT, Windows 2000, or Windows XP. Pay attention

The DirectoryEntry constructor passes the string starting with "Winnt: //". You can also run "Directory Service" on other third-party operating systems. DirectoryEntry Ad = New DirectoryEntry ("Winnt: //" SystemInformation.computername ", Computer");

Add a directory tree to the catalog item below

Action added in the Active Directory tree

User type, value

Testuser1

DirectoryEntry.

DirectoryEntry newuser = ad.children.add ("testuser1", "user");

Set the password for the new user account and describe the following code call

INVOKE method to call

DirectoryEntry object

SetPassword and

PUT method. This will set a password for the user account and assign a description. This code is also called

The commitChanges method saves these changes.

Newuser.invoke ("setPassword", new object [] {"# 12345abc"});

Newuser.invoke ("put", new object [] {"description", "test user from .net"});

Newuser.commitchanges ();

The first step in adding an account to a group to add an account to a group to define

DIRECTORYENTRY variable. Then call

ActiveDirectory class

Children member

Fill the variable. In this case, the Guest group is a search target. This code test

The value returned by the Find method to determine if the group has been found. If you find the group, the new user account will be added to the group.

DirectoryEntry GRP;

Grp = ad.children.find ("Guests", "group");

IF (GRP! = null) {grp.invoke ("add", new object [] {newuser.path.tostring ()});

Back to top

Troubleshooting If you try to run the code in this article, there is not enough permissions to create a user account, these code will fail. To make these code successfully completed, the currently logged in user must be a member of the Administrators group or have specific permissions to create user accounts.

Back to top

The information in this article applies to:

Microsoft Visual C # .NET (2002)

Recent Updated: 2002-2-24 (1.0) Keyword Kbhowto KbhowTomaster KB306273

转载请注明原文地址:https://www.9cbs.com/read-108953.html

New Post(0)