上一篇:使用C#批量修改域帐户信息全部代码 >>
使用C#批量修改域帐户信息
我创建了一个windows应用程序来解决:
在Form上放置了六个comboBox用于选择公司的OU(可以选择五级OU,如果你喜欢可以用TreeView更好些)。
加载Form的时候初始化这些comboBox:
private void Form1_Load(object sender, System.EventArgs e)
{
//初始化公司选择框
DirectoryEntry de1=new DirectoryEntry();
de1.Path="LDAP://DC=test,DC=net";
try
{
//所有OU(第一级)
foreach (DirectoryEntry ch1 in de1.Children)
{
str=ch1.Name.ToString();
string str1="";
//str1=str.Substring(0,str.IndexOf("="));
str1=ch1.SchemaClassName.ToString();
if (str1=="organizationalUnit")
{
//listBox1.Items.Add(ch1.Name.ToString());
//加入第一个combobox
comboBox1.Items.Add(ch1.Name.ToString());
//
comboBox3.Items.Add(ch1.Name.ToString());
}
}
de1.Close();
//textBox1.Text=textBox1.Text+"--------------next------------------------\r\n";
//
MessageBox.Show("finish!!!");
}
catch(Exception ex)
{
strErr=ex.Message;
}
finally
{}
}
在初始化form的时候在第一个combobox中显示出所有的第一层OU。然后,在选择了这个combobox的时候,在第二个combobox中显示下一级OU:
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//str=listBox1.SelectedItem.ToString();
str=comboBox1.SelectedItem.ToString();
DirectoryEntry de1=new DirectoryEntry();
de1.Path="LDAP://"+str+",DC=test,DC=net";
try
{
comboBox2.Items.Clear();
comboBox2.Text="";
comboBox2.Refresh();
foreach (DirectoryEntry ch1 in de1.Children)
{
//
textBox1.Text=textBox1.Text+str+"\r\n";//ch.Properties["adpath"][0].ToString();
string str1="";
str1=ch1.SchemaClassName.ToString();
if (str1=="organizationalUnit")
{
comboBox2.Items.Add(ch1.Name.ToString());
}
}
de1.Close();
//textBox1.Text=textBox1.Text+"--------------next------------------------\r\n";
//
MessageBox.Show("finish!!!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{}
}
依次在下一个combobox中显示下一级OU。选择好要修改的OU下后,就可以修改该ou下所有的信息了。这里以修改部门名称为例。
使用一个Textbox输入部门名称,放一个按钮触发修改动作:
private void button1_Click(object sender, System.EventArgs e)
{
string strADRoot="";
string strName="";
//str中保存的是OU的ADPath的一部分,即通过选择combobox产生的字符串,类似于ou=imd,OU=company
strADRoot="LDAP://"+str+",DC=test,DC=net";
DirectoryEntry de=new DirectoryEntry();
de.Path=strADRoot;
//修改所有的user对象
foreach(DirectoryEntry chm in de.Children)
{
string strType="";
strType=chm.SchemaClassName.ToString();
if(strType.ToUpper()=="USER")
{
strName=chm.Name.ToString();
//判断是否属性值是否为空
if(chm.Properties.Contains("department"))
{
chm.Properties["department"][0]=textBox2.Text.ToString();
chm.CommitChanges();
textBox3.Text=textBox3.Text+chm.Name .ToString()+"的部门名称修改成功!\r\n";
}
else
{
chm.Properties["department"].Add(textBox2.Text.ToString());
chm.CommitChanges();
//textBox3.Text=textBox3.Text+ch1.Name .ToString()+"\r\n";
textBox3.Text=textBox3.Text+chm.Name .ToString()+"的部门名称添加成功!\r\n";
}
}
}
下一篇:C#字符串的使用笔记 >>
相关文章:
- · 学C#遇到的几个问题
- · 磁盘配额的wmi版本(C#)
- · C#的四个基本技巧
- · 用C#开发opengl
- · 企业信息化系统基础——AD:使用C#批量创建帐号
- · 使用C#编程将websphere MQ 5.3 windows客户端消息发送到linux服务器端
- · 设置Windows系统NTFS某个目录的用户访问权限(c#)
- · 六种快速修理C# Bug的方法
- · 自己动手用c#写控件
- · NET 中,使用 GDI 来精确测量文本的实际绘出尺寸(C#)
- · 关于Visual C#装箱与拆箱的研究
- · C#中调用消息对话框,并取得其返回值
- · 在C#中应用HOOK
- · C#字符串的使用笔记
- · 解读 C# 中的正则表达式
- · 用正则表达式解析C#文件(updated)
- · C#中using关键字的使用
- · Vsiaul C#如何读取注册信息
- · 使用WMI列出Windows中某个目录的用户权限(C#)
- · Visual C#中的多线程编程
- · 在Windows下让不同用户使用不同的分辨率(C# 2005)
- · Regular Expression 正则表达式-1 (C#)
- · 一步一步用Visual C#创建Web服务
- · C# 2.0 匿名方法与 Windows Forms 异步调用
- · 由C#风潮想起的-给初学编程者的忠告
- · c#中结构与类的区别
- · C#初学乍练-文本替换工具命令行版
- · 点注《C# Coding Standard》Charpter One
- · 使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表
- · 软件架构(C#)
- · IDesign C#编码规范(总结)
- · IDesign C#编码规范(之十一)
- · IDesign C#编码规范(之十)
- · IDesign C#编码规范(之九)
- · IDesign C#编码规范(之八)
- · IDesign C#编码规范(之七)
- · IDesign C#编码规范(之六)
- · 在C#中如何在客户端接收信件
