现在单位的部门(包括子部门)有上百个,之前做了一个管理系统已经有把机构存入数据库了,层次结构都有了,能否用编程方式读取数据库的数据,然后对应在域中添加OU,免得一个一个手动输入?不够分可以再加。
解决方案 »
- 如何声明带Dictionary值带类型参数
- C#怎样调用VC程序?
- C#如何写一个可以注册到系统的dll
- 如何在.Net中调用Java API,COM组件,COM+组件?
- 将数据从DataGridView导入到Execel中抛出奇怪异常!! 苦苦思索,毫无进展,希望高手指点
- $$$$$$$$$$$$高分求 工资管理系统!!!!!!!!
- 急,不知道怎么改!
- 老话题&新问题(关于c#调用c++dll的问题)
- 作用域的问题
- 如何将XML文档发到请求的客户端?500分,决不失言?
- 怎样从搜索出来的2个记录中的某列相加?小女子没有分了,大家帮帮我
- vs2005中的Web Site Administration Tool 遇到的问题
程序如下:
private void button1_Click(object sender, System.EventArgs e)
{
DirectoryEntry entry;
// "WinNT://Domain"
String strPath=@"WinNT://dpsd";
try
{
entry=new DirectoryEntry(strPath);
TreeNode users = new TreeNode("Users");
TreeNode groups = new TreeNode("Groups");
TreeNode services = new TreeNode("Services");
viewPC.Nodes.Clear(); // viewPC is a TreeView Componet
viewPC.Nodes.AddRange(new TreeNode[] { users, groups, services }); foreach(System.DirectoryServices.DirectoryEntry child
in entry.Children)
{
TreeNode newNode = new TreeNode(child.Name);
switch (child.SchemaClassName)
{
case "User" :
users.Nodes.Add(newNode);
break;
case "Group" :
groups.Nodes.Add(newNode);
break;
case "Service" :
services.Nodes.Add(newNode);
break;
}
AddProperties(newNode, child);
}
}
catch (Exception ex)
{
MessageBox.Show( "err:" + ex.Message);
}
}private void AddProperties(TreeNode node, System.DirectoryServices.DirectoryEntry entry)
{
node.Nodes.Add(new TreeNode("Path: " + entry.Path));
TreeNode propertyNode = new TreeNode("Properties");
node.Nodes.Add(propertyNode);
foreach (string propertyName in entry.Properties.PropertyNames)
{
string oneNode = propertyName + ": " +
entry.Properties[propertyName][0].ToString();
propertyNode.Nodes.Add(new TreeNode(oneNode));
}
}
获取或设置此 DirectoryEntry 的路径。Path 属性唯一地标识网络环境中的此项。始终可以使用此 Path 检索此项。设置 Path 将从目录存储区检索新项;它不更改当前绑定的项的路径。与 DirectoryEntry 组件关联的类可以与任何 Active Directory 服务提供程序一起使用。当前的一些提供程序包括 Internet 信息服务 (IIS)、轻量目录访问协议 (LDAP)、Novell NetWare 目录服务 (NDS) 和 WinNT。注意 Path 的一部分,它标识提供程序(在“://”前面),并且区分大小写。例如,“LDAP://”或“WinNT://”。
Path 属性的语法随提供程序不同而不同。一些常见的情况有:WinNT 连接到计算机上的组。例如“WinNT://<域名>/<计算机名>/<组名>”。如果是连接到本地计算机,则为“WinNT://<计算机名>/<组名>”。
连接到计算机上的用户。例如“WinNT://<域名>/<计算机名>/<用户名>”。如果是连接到本地计算机,则为“WinNT://<计算机名>/<用户名>”。
连接到计算机上的服务。例如,“WinNT://<域名>/<计算机名>/<服务名>”。如果是连接到本地计算机,则为“WinNT://<计算机名>/<服务名>”。
发现网络上的所有域。例如,“WinNT:”。通过枚举此项的子级可以找到这些域。
LDAP 连接到域中的组。例如“LDAP://CN=<组名>, CN =<用户>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
连接到域中的用户。例如“LDAP://CN=<完整用户名>, CN=<用户>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
连接到域中的计算机。例如“LDAP://CN=<计算机名>, CN=<计算机>, DC=<域控制器 1>, DC=<域控制器 2>,...”。
IIS 连接到 Web 目录。例如“IIS://LocalHost/W3SVC/1/ROOT/<Web 目录名>”。
若要使用 LDAP 绑定到当前域,请使用路径“LDAP://RootDSE”,然后获取默认命名上下文,并重新绑定该项。例如:[C#]
String str = ent.Properties["defaultNamingContext"][0];
DirectoryEntry domain = new DirectoryEntry("LDAP://" + str);
2.-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
WinNT ADsPathThe ADsPath string for the ADSI WinNT provider can be one of the following forms:
"WinNT:"
"WinNT://<domain name>"
"WinNT://<domain name>/<server>"
"WinNT://<domain name>/<path>"
"WinNT://<domain name>/<object name>"
"WinNT://<domain name>/<object name>,<object class>"
The domain name can be either a NETBIOS name or a DNS name.The server is the name of a specific server within the domain.The path is the path of on object, such as "printserver1/printer2".The object name is the name of a specific object.The object class is the class name of the named object. One example of this usage would be "WinNT://MyDomain/JeffSmith,user". Specifying a class name can improve the performance of the bind operation.
你实现了“读”功能,但我要导入ou,就是要涉及到写,如何“写”?
{
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add(strLogin, "User");
//obUser.Properties["FullName"].Add("Amigo");
object obRet = obUser.Invoke("SetPassword", strPwd);
obUser.CommitChanges();
}
catch (Exception ex)
{
Trace.Warn(ex.Message);
}
}当然上栏列出的资料也列出了更改密码的方法。
using System;
using System.DirectoryServices;namespace ADAM_Examples
{
class CreateOU
{
/// <summary>
/// Create ADAM Organizational Unit.
/// </summary>
[STAThread]
static void Main()
{
DirectoryEntry objADAM; // Binding object.
DirectoryEntry objOU; // Organizational unit.
string strDescription; // Description of OU.
string strOU; // Organiztional unit.
string strPath; // Binding path. // Construct the binding string.
strPath = "LDAP://localhost:389/O=Fabrikam,C=US"; Console.WriteLine("Bind to: {0}", strPath); // Get ADAM object.
try
{
objADAM = new DirectoryEntry(strPath);
objADAM.RefreshCache();
}
catch (Exception e)
{
Console.WriteLine("Error: Bind failed.");
Console.WriteLine(" {0}", e.Message);
return;
} // Specify Organizational Unit.
strOU = "OU=TestOU";
strDescription = "ADAM Test Organizational Unit";
Console.WriteLine("Create: {0}", strOU); // Create Organizational Unit.
try
{
objOU = objADAM.Children.Add(strOU,
"OrganizationalUnit");
objOU.Properties["description"].Add(strDescription);
objOU.CommitChanges();
}
catch (Exception e)
{
Console.WriteLine("Error: Create failed.");
Console.WriteLine(" {0}", e.Message);
return;
} // Output Organizational Unit attributes.
Console.WriteLine("Success: Create succeeded.");
Console.WriteLine("Name: {0}", objOU.Name);
Console.WriteLine(" {0}",
objOU.Properties["description"].Value);
return;
}
}
}