コード例 #1
0
        private void tvwMain_AfterExpand(object sender, TreeViewEventArgs e)
        {
            TreeNode cur = e.Node;

            if (cur.Tag.ToString().IndexOf(":", 1) > 0) //if its got a : it means its a C:\ like-type path etc
            {
                ExploreTreeNode(cur);
            }
            else if ((cur.Text == "My Computer") || (cur.Text == "Entire Network") ||
                     ((cur.Parent != null) && (cur.Parent.Text == "Entire Network")))
            {
                if (cur.Text == "My Computer")
                {
                    PopulateMyComputer(); //add each drive and files and dirs (if it hasnt already).
                    return;
                }
                Cursor.Current = Cursors.WaitCursor;
                if (cur.Text == "Entire Network")
                {
                    if (cur.FirstNode.Text == "Network Placeholder Node")
                    {
                        cur.FirstNode.Remove();

                        var servers = new ServerEnum(ResourceScope.RESOURCE_GLOBALNET,
                                                     ResourceType.RESOURCETYPE_DISK, ResourceUsage.RESOURCEUSAGE_ALL,
                                                     ResourceDisplayType.RESOURCEDISPLAYTYPE_NETWORK, "");

                        foreach (string s1 in servers)
                        {
                            string s2 = s1.Substring(0, s1.IndexOf("|", 1));

                            if ((s2 == "Microsoft Terminal Services") || (s2 == "Microsoft Windows Network"))
                            {
                                //ignore these names
                                continue;
                            }
                            //Domain or Workgroup
                            var nodeDorW = new TreeNode
                            {
                                Tag                = cur.Text + Seperator + s2,
                                Text               = s2,
                                ImageIndex         = 16,
                                SelectedImageIndex = 16
                            };
                            cur.Nodes.Add(nodeDorW);

                            //Placeholder supposed to hold computers.
                            var nodeNcmp = new TreeNode
                            {
                                Tag                = "NCMP",
                                Text               = "NCMP",
                                ImageIndex         = 12,
                                SelectedImageIndex = 12
                            };
                            nodeDorW.Nodes.Add(nodeNcmp);
                        }
                    }
                }
                if ((cur.Parent != null) && (cur.Parent.Text == "Entire Network"))
                {
                    if (cur.FirstNode.Text == "NCMP")
                    {
                        cur.FirstNode.Remove(); //remove the NCMP placeholder

                        string pS = cur.Text;

                        var allservers = new ServerEnum(ResourceScope.RESOURCE_GLOBALNET,
                                                        ResourceType.RESOURCETYPE_DISK, ResourceUsage.RESOURCEUSAGE_ALL,
                                                        ResourceDisplayType.RESOURCEDISPLAYTYPE_SERVER, pS);

                        IEnumerator e1 = allservers.GetEnumerator(); //e1 is enumerator.
                        //new array of computer nodes, with their subnode shares. (yes there will be blank indexes)
                        var computerlist = new TreeNode[allservers.Count];
                        int i            = 0; //accumulator
                        int lasti        = 0; //last location of a computer in the computerlist
                        while (e1.MoveNext())
                        {
                            if (e1.Current != null)
                            {
                                var enumtext = e1.Current.ToString();
                                // if NOT a _share, then its a Network Computer, and we add it
                                if (!enumtext.EndsWith("_share"))
                                {
                                    var aComp = new TreeNode
                                    {
                                        Tag                = enumtext,
                                        Text               = enumtext.Substring(2),
                                        ImageIndex         = 12,
                                        SelectedImageIndex = 12
                                    };
                                    computerlist[i] = aComp;
                                    lasti           = i;
                                }
                                else //Network Computer Shares subnodes.
                                {
                                    var pos       = enumtext.LastIndexOf("\\") + 1;
                                    var aSubShare = new TreeNode
                                    {
                                        Tag                = enumtext.Substring(0, enumtext.Length - 6),
                                        Text               = enumtext.Substring(pos, enumtext.Length - pos - 6),
                                        ImageIndex         = 28,
                                        SelectedImageIndex = 28
                                    };
                                    computerlist[lasti].Nodes.Add(aSubShare);
                                }
                            }
                            i++;
                        }
                        foreach (TreeNode eachcomp in computerlist)
                        {
                            if (eachcomp != null)
                            {
                                cur.Nodes.Add(eachcomp);
                            }
                        }
                    }
                }
            }
            Cursor.Current = Cursors.Default;
        }