using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Collections; using System.Windows.Forms; namespace TestWForm { public partial class Form1 : Form { // Create a new ArrayList to hold the ourpath objects. private ArrayList ourpArray = new ArrayList(); private ArrayList ourfArray = new ArrayList(); private ArrayList ourpathArray = new ArrayList(); private ArrayList ourfileArray = new ArrayList(); private bool atStart = true; private string lastPath = "."; public Form1() { InitializeComponent(); } private TreeNode GoBackAllTheWay(TreeNode intnode, string inpath, int offset) { string outpath = inpath, xshuh = ""; int ioff = 0; TreeNode outtnode = intnode; DirectoryInfo xmydinfy = new DirectoryInfo(inpath); if (offset < 0) { ourpArray.Clear(); ourpArray.Add(xmydinfy.FullName + "\\"); foreach (DirectoryInfo inxdir in xmydinfy.GetDirectories()) { if (outpath.Length == inpath.Length) outpath = inxdir.FullName + "\\"; ourpArray.Add(inxdir.FullName + "\\"); } offset = -offset; outtnode.Nodes.Add(new TreeNode(outpath)); offset = outtnode.GetNodeCount(false); TreeNode tn = null; foreach (DirectoryInfo dinfile in xmydinfy.GetDirectories()) { if (dinfile.FullName.Length > inpath.Length) { outpath = dinfile.FullName + "\\"; if (offset <= 0 && 1 == 2) { outtnode.Nodes.Add(new TreeNode(outpath)); offset = outtnode.GetNodeCount(false); } else { tn = new TreeNode(outpath); offset = outtnode.GetNodeCount(false); // = ourpArray.LastIndexOf(outpath); } ourfArray.Clear(); DirectoryInfo zmydinfy = new DirectoryInfo(outpath); foreach (FileInfo inxfile in zmydinfy.GetFiles()) { xshuh = (inxfile.FullName + "\\").Substring(0, (inxfile.FullName + "\\").LastIndexOf("\\")); if (!System.IO.Directory.Exists(inxfile.FullName)) ourfArray.Add(inxfile.FullName); tn.Nodes.Add(new TreeNode(inxfile.FullName.Substring(outpath.Length))); offset = outtnode.GetNodeCount(false); } outtnode.Nodes[outtnode.GetNodeCount(false) - 1 + ioff * 0].Nodes.Add(tn); offset = outtnode.GetNodeCount(false); ioff++; } } if (intnode == outtnode || inpath == outpath) return outtnode; return GoBackAllTheWay(outtnode, outpath, offset); } private void ProcessDir(string myd, int oktodelve) { if (myd == "") myd = "."; bool lastAtStart = atStart; if (!atStart) lastPath = myd; atStart = true; string pathbit = ""; string shuh = ""; int ij = 0, kj = -1; TreeNode subtree = null; // Add a root from the ArrayList. ` foreach (String ourpath in ourpathArray) { ourfileArray.Clear(); DirectoryInfo mydinfy = new DirectoryInfo(ourpath); foreach (FileInfo infile in mydinfy.GetFiles()) { shuh = (infile.FullName + "\\").Substring(0, (infile.FullName + "\\").LastIndexOf("\\")); if (ourfileArray.IndexOf(shuh) < 0 && shuh != mydinfy.FullName && System.IO.Directory.Exists(infile.FullName)) { ProcessDir(shuh,0); break; } if (!System.IO.Directory.Exists(infile.FullName)) ourfileArray.Add(infile.FullName); } if (atStart) { lastPath = ourpath; pathbit = ourpath; MyTreeView1.Nodes.Add(new TreeNode(ourpath)); } else { if (!lastAtStart) { kj = ourpathArray.LastIndexOf(lastPath); pathbit = ourpath; subtree = new TreeNode(ourpath); } else { MyTreeView1.Nodes.Add(new TreeNode(ourpath)); } lastPath = ourpath; } lastAtStart = false; atStart = false; // Add a child treenode from sub-ArrayList. foreach (String ourfile in ourfileArray) { try { if (subtree != null) subtree.Nodes.Add(new TreeNode(ourfile.Substring(pathbit.Length))); else { MyTreeView1.Nodes[ourpathArray.LastIndexOf(ourpath)].Nodes.Add(new TreeNode(ourfile.Substring(pathbit.Length))); } } catch (Exception ex) { } } if (subtree != null) { MyTreeView1.Nodes[kj].Nodes.Add(GoBackAllTheWay(subtree, ourpath, -kj)); subtree = null; } } atStart = false; } private void Form1_Load(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; DirectoryInfo premydinfy = new DirectoryInfo("."); MyTreeView1.BeginUpdate(); MyTreeView1.Nodes.Clear(); ourpathArray.Clear(); ourpathArray.Add(premydinfy.FullName + "\\"); foreach (DirectoryInfo indir in premydinfy.GetDirectories(".")) { ourpathArray.Add(indir.FullName + "\\"); } ProcessDir(".",1); Cursor.Current = Cursors.Default; MyTreeView1.EndUpdate(); } } }