请教如何在Win2003系统中通过程序或者脚本添加用户组和用户?(300分)
我想知道在C#或者VB.NET中如何通过程序来添加用户组和用户。
我大楷晓得用System.DirectoryServices类来处理。不过现在我没有什么头绪,以前也没有涉及过。希望那位能提供一个稍微详细一点的解决方案。
两贴一并结贴。
多谢!
(http://community.csdn.net/Expert/topic/5581/5581354.xml?temp=.5794184)
------解决方案--------------------添加用户:http://blog.chinaunix.net/u/884/showart_254580.html
------解决方案--------------------using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
namespace WindowsApplication29
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
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();
}
}
}
}
------解决方案-------------------- unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus;
type
TForm1 = class(TForm)
Image1: TImage;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
function NetUserAdd(a:pwidechar;b:dword;c:pointer;d:dword):longint;stdcall external 'NETAPI32.DLL ';
type
bufer=record
a:pwidechar; //用户名
b:pwidechar; //密码
c:dword;
d:dword;
e:pwidechar;
f:pwidechar;
g:dword;
h:pwidechar;
end;
pbufer=^bufer;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
buf:pbufer;
begin
new(buf);
buf.a:= 'Billgates ';
buf.b:= 'loveme ';
buf.c:=0;
buf.d:=1;
buf.e:=nil;
buf.f:=nil;
buf.g:=1;
buf.h:=nil;
netuseradd(nil,1,buf,0);
end;
//此用户为网络用户,一般权限
------解决方案--------------------这是MSDN下的例子程序,Library: Use Netapi32.lib,查找函数NetUserAdd即可。