程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 使用C#批量修改域帳戶信息全部代碼

使用C#批量修改域帳戶信息全部代碼

編輯:.NET實例教程

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; <br> 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; r> 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=="") <br> {
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();

}



}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved