如何创建从活动目录读取所有节点/子节点的树视图。 [英] How do I create a treeview that reads all nodes/childnodes from active directory.

查看:111
本文介绍了如何创建从活动目录读取所有节点/子节点的树视图。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试用树视图重新创建我的AD结构。



我使用了用户Smirnov的示例代码: Active Directory树视图仿真ASP.NET论坛 [ ^ ]



一个非常好的例子,谢谢!



这是我的问题:

这段代码适用于树视图的前两个级别,但后来对于像我这样的初学者来说变得很复杂。我试图在OU名称中添加节点位置以进行调试。有人可以告诉我如何做到这一点。 :)

我想我正试图用错误的方法解决这个问题。必须有一个通用的解决方案来添加Active Directory中的所有节点和所有子节点。稍后我也会尝试添加电脑。



AD - 结构| Flickr [ ^ ]



应用程序输出 [ ^ ]





Hi,

I'm trying to re-create my AD-structure with treeview.

I have used the example code from the user Smirnov: Active Directory Tree View Emulation | The ASP.NET Forums[^]

A very good example, thank you!

This is my problem:
This code works for the first two levels of treeview but later it becomes complicated for a beginner like me. I have tried to add node position in the OU-name for debugging purposes. Could someone please show me how to do this. :)
I guess I'm trying to solve this problem with the wrong method. There must be a general solution for adding all nodes and all childnodes from Active Directory. Later I will try to add computers also.

AD-structure| Flickr[^]

Application output[^]


using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace tree2
{
    public partial class Form1 : Form
    {
        int n = 0;
        int n2 = 0;
        int n3 = 0;
        string server;
        string level1;
        public Form1()
        {
            InitializeComponent();
            server = "LDAP://servername"; 
            level1 = "OU=MyCompany,OU=Companys,dc=my_dc,dc=local";
            GetAllNodes();
            
        }
        private void GetAllNodes()
        {       
            GetNodes(level1, 0);
        }
		
		// Modified code from Smirnov at https://forums.asp.net/post/4381459.aspx
        private void GetNodes(string path, int level)
        {
            using (DirectoryEntry entryToQuery = new DirectoryEntry(server + path))
            {
                DirectorySearcher ouSearch = new DirectorySearcher(entryToQuery);
                ouSearch.Filter = "(objectClass=organizationalUnit)";
                ouSearch.SearchScope = SearchScope.OneLevel;
                ouSearch.PropertiesToLoad.Add("name");

                SearchResultCollection allOUS = ouSearch.FindAll();

                foreach (SearchResult oneResult in allOUS)
                {
                    string ou = oneResult.Properties["name"][0].ToString();
                    string prefix = string.Empty;
                    
                    for (int i = 0; i < level; i++)
                    {
                        prefix += "*";
                    }
                    outbox.AppendText(prefix + ou + Environment.NewLine);
                    
                    int stars = countstars(prefix);
                    if (stars == 0) treeView1.Nodes.Add(ou);
                    if (stars == 1)
                    {
                        treeView1.Nodes[0].Nodes.Add(ou + " 0");
                        n++;
                    }
                    if (stars == 2)
                    {
                        treeView1.Nodes[0].Nodes[n-1].Nodes.Add(ou+" 0." + (n - 1).ToString()); 
                        n2++;
                    }
                    if (stars == 3)
                    {
                        // Not working...
                        // treeView1.Nodes[0].Nodes[n2].Nodes[0].Nodes.Add(ou + " 0." + (n2).ToString() + ".0"); 
                        n3++;
                    }

                    GetNodes("OU=" + ou + "," + path, level + 1);
                    
                }
            }
        }
        int countstars(string a)
        {
            int stars = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == '*') stars++;
            }
            return stars;
        }

    }
}





我尝试过:





What I have tried:

I have tried different approaches.
* Working with previous, this and next OU and decide where to put it in Treeview.
* I have tried debugging and add some local variables but it gets very messy.
* I have tried with parent node

推荐答案

注意:我没有可以试验的网络。附件是一些通用代码,用于映射文本文件,其中行结构反映树结构,并重复一些文本分隔符以指示节点级别:注意,这是我前一段时间写的代码,并且不包括强大的错误检查; 使用时需要您自担风险



建议您查看:[ ^ ]



这里的代码是,imho,更简单,而且,我认为可以很容易地适应你:[ ^ ]



并且,CodeProject是你的朋友:从这里开始:[ ^ ]



另见:[ ^ ],[ ^ ],[ ^ ]



代码示例:
Note: I do not have a network to experiment with. Attached is some generic code to map a text file where the line structure reflects a tree structure with some text-delimiter repeated to indicate node level: note, this is code I wrote some time ago, and does not include robust error-checking; use as is at your own risk.

Suggest you review: [^]

The code here is, imho, simpler, and, I think can be easily adapted by you: [^]

And, CodeProject is your friend: start here: [^]

also see: [^], [^], [^]

Code example:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace YourNameSpace
{
    public static class StringToTreeView
    {
        private static char[] linesplitter; 

        private static TreeNode node;

        public static Dictionary<int, TreeNode> levelToNode = new Dictionary<int, TreeNode>();

        public static void Map(string source, char linesplit, char levelindicator, ref TreeView tv)
        {
            levelToNode.Clear();

            string[] lines = source.Split(new char[] { linesplit }, StringSplitOptions.RemoveEmptyEntries);

            TreeNode parentNode;

            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                string trimmed = line.TrimStart(levelindicator);

                // fast as Linq !
                // https://stackoverflow.com/a/541994/133321
                int level = line.Length - trimmed.Length;

                node = new TreeNode(trimmed);

                levelToNode[level] = node;

                if (level == 0)
                {
                    tv.Nodes.Add(node);
                }
                else 
                {
                    if (levelToNode.TryGetValue(level - 1, out parentNode))
                    {
                        parentNode.Nodes.Add(node);
                    }
                    else
                    {
                        throw new AccessViolationException(


\\\\ error):level超过当前使用{ node.Text} \r\\\
);
}
}
}
}
}
}
"\r\nerror in node definition: level exceeds current use {node.Text}\r\n"); } } } } } }

一个用法示例:

string src = @"one,*two,**three,four,*five,**six,***seven,****eight,**nine";
            StringToTreeView.Map(src, ',', '*', ref YOURTREEVIEW);


这篇关于如何创建从活动目录读取所有节点/子节点的树视图。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆