Example #1
0
        public void Recursion(TreeGridView tgv, TreeGridNode node, string path, string top, Dictionary <string, TreeItem> dic)
        {
            bool isFirst = false;

            if (node == null)
            {
                isFirst = true;
            }
            Font          boldFont = new Font(tgv.DefaultCellStyle.Font, FontStyle.Bold);
            DirectoryInfo dir      = new DirectoryInfo(path);

            if (dir.Exists)
            {
                DirectoryInfo[] dirs = dir.GetDirectories();
                if (dirs.Length > 0)
                {
                    foreach (var each in dirs)
                    {
                        string folderKey = each.FullName.Replace(top, "");
                        if (isFirst)
                        {
                            node                       = tgv.Nodes.Add(null, each.Name, "", each.LastWriteTime, each.FullName);
                            node.ImageIndex            = 0;
                            node.DefaultCellStyle.Font = boldFont;
                            List <int> indexList = new List <int>();
                            indexList.Add(tgv.Nodes.Count - 1);
                            TreeItem item = new TreeItem(null, each.Name, 0, each.LastWriteTime, each.FullName, 0, indexList, node.Level);
                            dic.Add(folderKey, item);
                            Recursion(tgv, node, each.FullName, top, dic);
                        }
                        else
                        {
                            TreeGridNode childNode = node.Nodes.Add(null, each.Name, "", each.LastWriteTime, each.FullName);
                            childNode.ImageIndex            = 0;
                            childNode.DefaultCellStyle.Font = boldFont;
                            string     parentKey = each.Parent.FullName.Replace(top, "");
                            List <int> indexList = new List <int>();
                            indexList.AddRange(dic[parentKey].IndexList);
                            indexList.Add(node.Nodes.Count - 1);
                            TreeItem item = new TreeItem(null, each.Name, 0, each.LastWriteTime, each.FullName, 0, indexList, childNode.Level);
                            dic.Add(folderKey, item);
                            Recursion(tgv, childNode, each.FullName, top, dic);
                        }
                    }
                }
                FileInfo[] files = dir.GetFiles();
                if (files.Length > 0)
                {
                    foreach (var each in files)
                    {
                        string fileKey  = each.FullName.Replace(top, "");
                        double tempSize = (double)each.Length / 1024;
                        long   size     = each.Length / 1024;
                        if (tempSize > size)
                        {
                            size++;
                        }
                        if (isFirst)
                        {
                            node            = tgv.Nodes.Add(null, each.Name, size + "KB", each.LastWriteTime, each.FullName);
                            node.ImageIndex = 1;
                            List <int> indexList = new List <int>();
                            indexList.Add(tgv.Nodes.Count - 1);
                            TreeItem item = new TreeItem(null, each.Name, each.Length, each.LastWriteTime, each.FullName, 1, indexList, node.Level);
                            dic.Add(fileKey, item);
                        }
                        else
                        {
                            TreeGridNode childNode = node.Nodes.Add(null, each.Name, size + "KB", each.LastWriteTime, each.FullName);
                            childNode.ImageIndex = 1;
                            string     parentKey = each.Directory.FullName.Replace(top, "");
                            List <int> indexList = new List <int>();
                            indexList.AddRange(dic[parentKey].IndexList);
                            indexList.Add(node.Nodes.Count - 1);
                            string   folderName = each.DirectoryName.Replace(top, "");
                            TreeItem item       = new TreeItem(folderName, each.Name, each.Length, each.LastWriteTime, each.FullName, 1, indexList, childNode.Level);
                            dic.Add(fileKey, item);
                        }
                    }
                }
            }
            else
            {
                throw new Exception("Directory is not exists.");
            }
        }