编写自动化代码的朋友经常会遇到添加,删除组,用户之类的操作,我们通过命令行的net localgroup /add 也可以实现,但是对钟爱.net的人来说,我们也有办法。看如下代码:
using System.DirectoryServices;
using System;
using System.Collections.Generic;
namespace ToolsKit
{
public class ADDGroup
{
static void Main(string[] args)
{
//通过命令行输入需要添加的组的名字和描述, 注意我这里使用空格来间隔
List<GroupInfo> groups=CreateList(args);
//初始化DirectoryEntry对象
DirectoryEntry root = new DirectoryEntry("WinNT://" +Environment.MachineName + ",computer");
try
{
//AddGroupByList(ref root,groups);
//DeleteGroup(root,"AeTLAS Administration");
//AddUserToGroup(root,"Guests");
DeleteUserFromGroup(root,"Guests","qigaopan");
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
root.Close();
}
/*ADD GROUPS*/
static void AddGroupByList(ref DirectoryEntry root,List<GroupInfo> list)
{
DirectoryEntry groups;
foreach(GroupInfo info in list)
{
groups= root.Children.Add(info.GroupName,"group");
groups.Properties["description"].Add(info.Description);
groups.CommitChanges();
}
}
/*Delete GROUPS*/
static void DeleteGroup(DirectoryEntry root,string Name)
{
DirectoryEntry group = root.Children.Find(Name);
if (group != null)
root.Children.Remove(group);
}
/*Modify a Group*/
static void ModifyGroup(DirectoryEntry root,string Name)
{
DirectoryEntry group = root.Children.Find(Name);
if (group != null)
{
group.Properties["description"].Value = "Change the description.";
group.Rename("NewName");
group.CommitChanges();
}
root.Close();
}
//ADD A USER
static void AddUser(DirectoryEntry root)
{
DirectoryEntry newUser = root.Children.Add("ab", "user");
newUser.Invoke("SetPassword", new object[] { "Wangwenyan1983!12^"});
newUser.Properties["description"].Add("This is a tested user");
newUser.CommitChanges();
}
/*Add a user to specified group*/
static void AddUserToGroup(DirectoryEntry root,string groupname)
{
DirectoryEntry newUser = root.Children.Add("qigaopan", "user");
newUser.Invoke("SetPassword", new object[] { "Wangwenyan1983!12^"});
newUser.Properties["description"].Add("This is a tested user");
newUser.CommitChanges();
DirectoryEntry grp=root.Children.Find(groupname,"group");
Console.WriteLine(grp.Name);
Console.WriteLine(newUser.ToString());
if(grp.Name!="")
{
grp.Invoke("Add",new Object[]{newUser.Path.ToString()});
}
Console.WriteLine("Account Created Successfully");
Console.ReadLine();
}
/*Delete a user from a specified group*/
static void DeleteUserFromGroup(DirectoryEntry root,string groupname,string username)
{
DirectoryEntry grp=root.Children.Find(groupname,"group");
Console.WriteLine(grp.Name);
DirectoryEntry newUser=root.Children.Find(username,"user");
Console.WriteLine(newUser.Name);
if (grp != null) {grp.Invoke("Remove", new object[] {newUser.Path.ToString()});}
}
static List<GroupInfo> CreateList(string[] source)
{
int len=source.Length;
List<GroupInfo> list=new List<GroupInfo>();
GroupInfo group;
for(int i=0;i<len;i++)
{
group=new GroupInfo(source[i],source[++i]);
list.Add(group);
}
return list;
}
}
class GroupInfo
{
string groupName;
string description;
public GroupInfo(string name,string descriptions)
{
this.groupName=name;
this.description=descriptions;
}
public string GroupName
{
get
{
return this.groupName;
}
}
public string Description
{
get
{
return this.description;
}
}
}
}
注释不是很多,但是因为代码比较简单,所以代码本身就是注释。