欢迎光临
我们一直在努力

使用C#批量修改域帐户信息全部代码-.NET教程,C#语言

建站超值云服务器,限时71元/月

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;

using system.directoryservices;

namespace change

{

/// <summary>

/// form1 的摘要说明。

/// </summary>

public class form1 : system.windows.forms.form

{

private system.windows.forms.combobox combobox2;

private system.windows.forms.label label3;

private system.windows.forms.combobox combobox1;

private system.windows.forms.label label2;

string str="";

string strerr="";

string strmanager="";

private system.windows.forms.label label1;

private system.windows.forms.label label4;

private system.windows.forms.button button1;

private system.windows.forms.textbox textbox1;

private system.windows.forms.textbox textbox2;

private system.windows.forms.textbox textbox3;

private system.windows.forms.checkbox checkbox1;

private system.windows.forms.checkbox checkbox2;

private system.windows.forms.checkbox checkbox3;

private system.windows.forms.label label5;

private system.windows.forms.textbox textbox4;

private system.windows.forms.combobox combobox3;

private system.windows.forms.label label6;

private system.windows.forms.combobox combobox4;

private system.windows.forms.label label7;

private system.windows.forms.combobox combobox5;

private system.windows.forms.label label8;

private system.windows.forms.combobox combobox6;

private system.windows.forms.label label9;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private system.componentmodel.container components = null;

public form1()

{

//

// windows 窗体设计器支持所必需的

//

initializecomponent();

//

// todo: 在 initializecomponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}

#region windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

this.combobox2 = new system.windows.forms.combobox();

this.label3 = new system.windows.forms.label();

this.combobox1 = new system.windows.forms.combobox();

this.label2 = new system.windows.forms.label();

this.label1 = new system.windows.forms.label();

this.label4 = new system.windows.forms.label();

this.button1 = new system.windows.forms.button();

this.textbox1 = new system.windows.forms.textbox();

this.textbox2 = new system.windows.forms.textbox();

this.textbox3 = new system.windows.forms.textbox();

this.checkbox1 = new system.windows.forms.checkbox();

this.checkbox2 = new system.windows.forms.checkbox();

this.checkbox3 = new system.windows.forms.checkbox();

this.label5 = new system.windows.forms.label();

this.textbox4 = new system.windows.forms.textbox();

this.combobox3 = new system.windows.forms.combobox();

this.label6 = new system.windows.forms.label();

this.combobox4 = new system.windows.forms.combobox();

this.label7 = new system.windows.forms.label();

this.combobox5 = new system.windows.forms.combobox();

this.label8 = new system.windows.forms.label();

this.combobox6 = new system.windows.forms.combobox();

this.label9 = new system.windows.forms.label();

this.suspendlayout();

//

// combobox2

//

this.combobox2.location = new system.drawing.point(416, 16);

this.combobox2.name = "combobox2";

this.combobox2.size = new system.drawing.size(121, 20);

this.combobox2.tabindex = 10;

this.combobox2.selectedindexchanged += new system.eventhandler(this.combobox2_selectedindexchanged);

//

// label3

//

this.label3.location = new system.drawing.point(320, 20);

this.label3.name = "label3";

this.label3.size = new system.drawing.size(80, 16);

this.label3.tabindex = 9;

this.label3.text = "选择部门(ou)";

//

// combobox1

//

this.combobox1.location = new system.drawing.point(144, 16);

this.combobox1.name = "combobox1";

this.combobox1.size = new system.drawing.size(121, 20);

this.combobox1.tabindex = 8;

this.combobox1.selectedindexchanged += new system.eventhandler(this.combobox1_selectedindexchanged);

//

// label2

//

this.label2.autosize = true;

this.label2.location = new system.drawing.point(40, 19);

this.label2.name = "label2";

this.label2.size = new system.drawing.size(79, 17);

this.label2.tabindex = 7;

this.label2.text = "选择公司(ou)";

//

// label1

//

this.label1.location = new system.drawing.point(48, 128);

this.label1.name = "label1";

this.label1.size = new system.drawing.size(72, 23);

this.label1.tabindex = 11;

this.label1.text = "新公司名称";

//

// label4

//

this.label4.location = new system.drawing.point(328, 128);

this.label4.name = "label4";

this.label4.size = new system.drawing.size(72, 23);

this.label4.tabindex = 12;

this.label4.text = "新部门名称";

//

// button1

//

this.button1.location = new system.drawing.point(376, 376);

this.button1.name = "button1";

this.button1.tabindex = 15;

this.button1.text = "开始处理";

this.button1.click += new system.eventhandler(this.button1_click);

//

// textbox1

//

this.textbox1.location = new system.drawing.point(144, 128);

this.textbox1.name = "textbox1";

this.textbox1.size = new system.drawing.size(120, 21);

this.textbox1.tabindex = 16;

this.textbox1.text = "";

//

// textbox2

//

this.textbox2.location = new system.drawing.point(416, 128);

this.textbox2.name = "textbox2";

this.textbox2.size = new system.drawing.size(120, 21);

this.textbox2.tabindex = 17;

this.textbox2.text = "";

//

// textbox3

//

this.textbox3.location = new system.drawing.point(32, 232);

this.textbox3.multiline = true;

this.textbox3.name = "textbox3";

this.textbox3.readonly = true;

this.textbox3.scrollbars = system.windows.forms.scrollbars.both;

this.textbox3.size = new system.drawing.size(504, 120);

this.textbox3.tabindex = 18;

this.textbox3.text = "";

//

// checkbox1

//

this.checkbox1.location = new system.drawing.point(64, 192);

this.checkbox1.name = "checkbox1";

this.checkbox1.tabindex = 20;

this.checkbox1.text = "修改公司名称";

//

// checkbox2

//

this.checkbox2.location = new system.drawing.point(200, 192);

this.checkbox2.name = "checkbox2";

this.checkbox2.tabindex = 21;

this.checkbox2.text = "修改部门名称";

//

// checkbox3

//

this.checkbox3.location = new system.drawing.point(336, 192);

this.checkbox3.name = "checkbox3";

this.checkbox3.tabindex = 22;

this.checkbox3.text = "修改部门领导";

//

// label5

//

this.label5.autosize = true;

this.label5.location = new system.drawing.point(40, 160);

this.label5.name = "label5";

this.label5.size = new system.drawing.size(79, 17);

this.label5.tabindex = 19;

this.label5.text = "新的部门领导";

//

// textbox4

//

this.textbox4.location = new system.drawing.point(144, 160);

this.textbox4.name = "textbox4";

this.textbox4.size = new system.drawing.size(392, 21);

this.textbox4.tabindex = 23;

this.textbox4.text = "请输入新领导的姓名全拼(域帐号)";

//

// combobox3

//

this.combobox3.location = new system.drawing.point(144, 48);

this.combobox3.name = "combobox3";

this.combobox3.size = new system.drawing.size(121, 20);

this.combobox3.tabindex = 25;

this.combobox3.selectedindexchanged += new system.eventhandler(this.combobox3_selectedindexchanged);

//

// label6

//

this.label6.autosize = true;

this.label6.location = new system.drawing.point(4, 51);

this.label6.name = "label6";

this.label6.size = new system.drawing.size(116, 17);

this.label6.tabindex = 24;

this.label6.text = "选择二级子部门(ou)";

//

// combobox4

//

this.combobox4.location = new system.drawing.point(416, 48);

this.combobox4.name = "combobox4";

this.combobox4.size = new system.drawing.size(121, 20);

this.combobox4.tabindex = 27;

this.combobox4.selectedindexchanged += new system.eventhandler(this.combobox4_selectedindexchanged);

//

// label7

//

this.label7.autosize = true;

this.label7.location = new system.drawing.point(284, 51);

this.label7.name = "label7";

this.label7.size = new system.drawing.size(116, 17);

this.label7.tabindex = 26;

this.label7.text = "选择三级子部门(ou)";

//

// combobox5

//

this.combobox5.location = new system.drawing.point(416, 80);

this.combobox5.name = "combobox5";

this.combobox5.size = new system.drawing.size(121, 20);

this.combobox5.tabindex = 31;

this.combobox5.selectedindexchanged += new system.eventhandler(this.combobox5_selectedindexchanged);

//

// label8

//

this.label8.location = new system.drawing.point(280, 84);

this.label8.name = "label8";

this.label8.size = new system.drawing.size(120, 16);

this.label8.tabindex = 30;

this.label8.text = "选择级五子部门(ou)";

//

// combobox6

//

this.combobox6.location = new system.drawing.point(144, 80);

this.combobox6.name = "combobox6";

this.combobox6.size = new system.drawing.size(121, 20);

this.combobox6.tabindex = 29;

this.combobox6.selectedindexchanged += new system.eventhandler(this.combobox6_selectedindexchanged);

//

// label9

//

this.label9.autosize = true;

this.label9.location = new system.drawing.point(4, 83);

this.label9.name = "label9";

this.label9.size = new system.drawing.size(116, 17);

this.label9.tabindex = 28;

this.label9.text = "选择四级子部门(ou)";

//

// form1

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(584, 429);

this.controls.add(this.combobox5);

this.controls.add(this.label8);

this.controls.add(this.combobox6);

this.controls.add(this.label9);

this.controls.add(this.combobox4);

this.controls.add(this.label7);

this.controls.add(this.combobox3);

this.controls.add(this.label6);

this.controls.add(this.textbox4);

this.controls.add(this.checkbox3);

this.controls.add(this.checkbox2);

this.controls.add(this.checkbox1);

this.controls.add(this.label5);

this.controls.add(this.textbox3);

this.controls.add(this.textbox2);

this.controls.add(this.textbox1);

this.controls.add(this.button1);

this.controls.add(this.label4);

this.controls.add(this.label1);

this.controls.add(this.combobox2);

this.controls.add(this.label3);

this.controls.add(this.combobox1);

this.controls.add(this.label2);

this.name = "form1";

this.text = "按ou成批修改信息";

this.load += new system.eventhandler(this.form1_load);

this.resumelayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[stathread]

static void main()

{

application.run(new form1());

}

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

{}

}

private void form1_load(object sender, system.eventargs e)

{

//初始化公司选择框

directoryentry de1=new directoryentry();

de1.path="ldap://dc=test,dc=net";

try

{

foreach (directoryentry ch1 in de1.children)

{

// textbox1.text=textbox1.text+ch1.name+"\r\n";//ch.properties["adpath"][0].tostring();

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());

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

{}

}

private void button1_click(object sender, system.eventargs e)

{

string stradroot="";

string strname="";

if(combobox1.text==""||combobox2.text=="")

{

strerr="请选择合适的ou";

goto e1;

}

if(checkbox1.checked)

{

if(textbox1.text=="")

{

strerr="请输入新公司名称";

goto e1;

}

}

if(checkbox2.checked)

{

if(textbox2.text=="")

{

strerr="请输入部门名称!";

goto e1;

}

}

if(checkbox3.checked)

{

if(textbox4.text=="请输入新领导的姓名全拼(域帐号)")

{

strerr="请输入新领导的姓名全拼(域帐号)!";

goto e1;

}

// string strnewmanager="";

//搜索

//检查是否有重复的帐号

directoryentry su=new directoryentry("ldap://dc=test,dc=net");

directorysearcher searcher = new directorysearcher();

searcher.searchroot=su;

searcher.propertiestoload.add("cn");

searcher.filter = "(&(objectclass=user)(samaccountname="+textbox4.text.tostring()+"))";

searcher.searchscope = searchscope.subtree;

searcher.sort = new sortoption("samaccountname", sortdirection.ascending);

searchresultcollection results = searcher.findall();

dialogresult dlresult;

string strmess="您确认要将该ou下的用户的领导更改为:"+results[0].properties["cn"][0].tostring();

dlresult=messagebox.show(this,strmess,"请确认",messageboxbuttons.yesno,

messageboxicon.question, messageboxdefaultbutton.button1,

messageboxoptions.rightalign);

if(dlresult == dialogresult.yes)

{

strmanager= results[0].properties["adspath"][0].tostring();

strmanager=strmanager.remove(0,7);

}

else

{

textbox4.text="请重新输入新领导的姓名全拼(域帐号)!";

strerr="请重新输入新领导的姓名全拼(域帐号)!";

goto e1;

}

}

if((!checkbox3.checked)&&(!checkbox2.checked)&&(!checkbox1.checked))

{

strerr="请选择要修改的项目!";

goto e1;

}

stradroot="ldap://"+str+",dc=test,dc=net";

directoryentry de=new directoryentry();

de.path=stradroot;

textbox3.text="";

try

{

foreach(directoryentry chm in de.children)

{

string strtype="";

strtype=chm.schemaclassname.tostring();

if(strtype.toupper()=="user")

{

strname=chm.name.tostring();

//如果选中了修改领导的话

if(checkbox3.checked)

{

if(chm.properties.contains("manager"))

{

chm.properties["manager"][0]=strmanager;

chm.commitchanges();

textbox3.text=textbox3.text+chm.name .tostring()+"的领导修改成功!\r\n";

}

else

{

chm.properties["manager"].add(strmanager);

chm.commitchanges();

textbox3.text=textbox3.text+chm.name .tostring()+"的领导添加成功!\r\n";

}

}

//修改公司

if(checkbox1.checked)

{

if(chm.properties.contains("company"))

{

chm.properties["company"][0]=textbox1.text.tostring();

chm.commitchanges();

textbox3.text=textbox3.text+chm.name .tostring()+"的公司名称修改成功!\r\n";

}

else

{

chm.properties["company"].add(textbox1.text.tostring());

chm.commitchanges();

textbox3.text=textbox3.text+chm.name .tostring()+"的公司名称添加成功!\r\n";

//textbox3.text=textbox3.text+ch1.name .tostring()+"\r\n";

}

}

//修改部门

if(checkbox2.checked)

{

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";

}

}

}

}

messagebox.show("修改成功!");

}

catch(exception ex)

{

strerr=ex.message;

goto e1;

}

e1: if(strerr!="")

{

messagebox.show(strerr);

}

}

private void combobox2_selectedindexchanged(object sender, system.eventargs e)

{

//str=listbox1.selecteditem.tostring();

str=combobox2.selecteditem.tostring()+","+combobox1.selecteditem.tostring();

directoryentry de1=new directoryentry();

de1.path="ldap://"+str+",dc=test,dc=net";

try

{

combobox3.items.clear();

combobox3.text="";

combobox3.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")

{

combobox3.items.add(ch1.name.tostring());

}

}

de1.close();

//textbox1.text=textbox1.text+"————–next————————\r\n";

// messagebox.show("finish!!!");

}

catch(exception ex)

{

messagebox.show(ex.message);

}

}

private void combobox3_selectedindexchanged(object sender, system.eventargs e)

{

str=combobox3.selecteditem.tostring()+","+combobox2.selecteditem.tostring()+","+combobox1.selecteditem.tostring();

directoryentry de1=new directoryentry();

de1.path="ldap://"+str+",dc=test,dc=net";

try

{

combobox4.items.clear();

combobox4.text="";

combobox4.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")

{

combobox4.items.add(ch1.name.tostring());

}

}

de1.close();

//textbox1.text=textbox1.text+"————–next————————\r\n";

// messagebox.show("finish!!!");

}

catch(exception ex)

{

messagebox.show(ex.message);

}

}

private void combobox4_selectedindexchanged(object sender, system.eventargs e)

{

str=combobox4.selecteditem.tostring()+","+combobox3.selecteditem.tostring()+","+combobox2.selecteditem.tostring()+","+combobox1.selecteditem.tostring();

directoryentry de1=new directoryentry();

de1.path="ldap://"+str+",dc=test,dc=net";

try

{

combobox6.items.clear();

combobox6.text="";

combobox6.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")

{

combobox6.items.add(ch1.name.tostring());

}

}

de1.close();

//textbox1.text=textbox1.text+"————–next————————\r\n";

// messagebox.show("finish!!!");

}

catch(exception ex)

{

messagebox.show(ex.message);

}

}

private void combobox6_selectedindexchanged(object sender, system.eventargs e)

{

str=combobox6.selecteditem.tostring()+","+combobox4.selecteditem.tostring()+","+

combobox3.selecteditem.tostring()+","+combobox2.selecteditem.tostring()+","+

combobox1.selecteditem.tostring();

directoryentry de1=new directoryentry();

de1.path="ldap://"+str+",dc=test,dc=net";

try

{

combobox5.items.clear();

combobox5.text="";

combobox5.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")

{

combobox5.items.add(ch1.name.tostring());

}

}

de1.close();

//textbox1.text=textbox1.text+"————–next————————\r\n";

// messagebox.show("finish!!!");

}

catch(exception ex)

{

messagebox.show(ex.message);

}

}

private void combobox5_selectedindexchanged(object sender, system.eventargs e)

{

str=combobox5.selecteditem.tostring()+","+combobox6.selecteditem.tostring()+","+combobox4.selecteditem.tostring()+","+

combobox3.selecteditem.tostring()+","+combobox2.selecteditem.tostring()+","+

combobox1.selecteditem.tostring();

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用C#批量修改域帐户信息全部代码-.NET教程,C#语言
分享到: 更多 (0)