none
如何用TreeView列出OU信息? RRS feed

  • 問題

  •  

    在網上就找下這一代碼,但只顯示user 和 group ,我想列出organizational unit, 不知怎樣實現呢?

    Code Snippet

            private void button1_Click(object sender, EventArgs e)
            {
                DirectoryEntry entry;
                //   "WinNT://Domain"  
                String strPath = @"WinNT://tdmsa";
                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, organizationalunit, 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));
                }
            }

     

     

    2008年11月17日 上午 04:37