private void files_AfterExpand(object sender, TreeViewEventArgs e) { List <TreeNode> Listing = new List <TreeNode>(); TreeNode node; new Thread((ThreadStart) delegate { FtpListItem[] items = IBMi.GetListing(e.Node.Tag.ToString()); foreach (FtpListItem item in items) { if (item.Name.Contains("/")) { int lastIndex = item.Name.LastIndexOf("/"); string tempName = item.Name.Substring(lastIndex + 1, item.Name.Length - lastIndex - 1); item.FullName = item.FullName.Substring(0, item.FullName.Length - item.Name.Length - 1) + "/" + tempName; item.Name = tempName; } FileCache.AddStreamFile(item.FullName); node = new TreeNode(item.Name); node.Tag = item.FullName; switch (item.Type) { case FtpFileSystemObjectType.Directory: node.ImageIndex = 0; node.SelectedImageIndex = 0; node.Nodes.Add(new TreeNode("Loading..", 2, 2)); Listing.Add(node); break; case FtpFileSystemObjectType.File: node.ImageIndex = 1; node.SelectedImageIndex = 1; Listing.Add(node); break; } } if (Listing.Count() == 0) { Listing.Add(new TreeNode("Directory is empty.", 2, 2)); } this.Invoke((MethodInvoker) delegate { e.Node.Nodes.Clear(); e.Node.Nodes.AddRange(Listing.ToArray()); }); }).Start(); }