TreeView控件操作开发篇之(五)

 
  #region 加载文件夹及文件到树

        /// <summary>
        /// 加载文件夹文件到树控件
        /// </summary>
        /// <param name="Tv">TreeView</param>
        /// <param name="DirPath">要加载的文件夹路径</param>
        /// <param name="ImgList">ImageList</param>
        /// <returns>True/False</returns>
        public bool LoadDirAndFiles(TreeView Tv, string DirPath, ImageList ImgList)
        {
            try
            {
                Tv.Nodes.Clear();
                Tv.ImageList = ImgList;
                //遍历文件夹
                foreach (DirectoryInfo Dir in new DirectoryInfo(DirPath).GetDirectories())
                {
                    TreeNode TN = new TreeNode();
                    TN.Text = Dir.Name;
                    TN.Tag = Dir.FullName;
                    SetTreeNodeIcon(TN, "Dir", ImgList);
                    Tv.Nodes.Add(TN);

                    ForLoadDirAndFiles(TN, Dir, ImgList);

                    //遍历文件夹下的文件 
                    foreach (FileInfo File in Dir.GetFiles())
                    {
                        TreeNode FTn = new TreeNode();
                        FTn.Text = File.Name;
                        FTn.Tag = File.FullName;
                        SetTreeNodeIcon(FTn, File.Extension, ImgList);
                        TN.Nodes.Add(FTn);
                    }
                }
                //遍历文件夹下的文件 
                foreach (FileInfo file in new DirectoryInfo(DirPath).GetFiles())
                {
                    TreeNode aTn = new TreeNode();
                    aTn.Text = file.Name;
                    aTn.Tag = file.FullName;
                    SetTreeNodeIcon(aTn, file.Extension, ImgList);
                    Tv.Nodes.Add(aTn);
                }
                return true;
            }
            catch 
            {
                return false;
            }
        }

        /// <summary>
        ///加载传入的文件夹文件列表
        /// </summary>
        /// <param name="CurrTn">树节点</param>
        /// <param name="CurrDir">文件夹</param>
        private void ForLoadDirAndFiles(TreeNode CurrTn, DirectoryInfo CurrDir, ImageList ImgList)
        {
            //遍历CurrDir文件夹下的文件夹
            foreach (DirectoryInfo aDir in CurrDir.GetDirectories()) 
            {
                TreeNode TN = new TreeNode();
                TN.Text = aDir.Name;
                TN.Tag = aDir.FullName;
                SetTreeNodeIcon(TN, "dir", ImgList);
                CurrTn.Nodes.Add(TN);
             
                // 遍历文件下的文件
                foreach (FileInfo File in aDir.GetFiles()) 
                {
                    TreeNode aNode = new TreeNode();
                    aNode.Text = File.Name;
                    aNode.Tag = File.FullName;
                    TN.Nodes.Add(aNode);
                    SetTreeNodeIcon(TN, File.Extension, ImgList);
                }
                ForLoadDirAndFiles(TN, aDir, ImgList);
            }
        }


        /// <summary>
        /// 设置节点的图标
        /// </summary>
        /// <param name="Tn">节点</param>
        /// <param name="strLX"></param>
        /// <param name="ImgList">图像</param>
        private void SetTreeNodeIcon(TreeNode Tn, string strLX,ImageList ImgList)
        {
             strLX = strLX.Replace('.',' ');
            if (strLX.ToLower() == "dir")  //目录
            {
                Tn.ImageIndex = ImgList.Images.IndexOfKey("close");
                Tn.SelectedImageIndex = ImgList.Images.IndexOfKey("open");
            }
            else
            {
                Tn.ImageIndex = ImgList.Images.IndexOfKey(strLX.Trim());
                Tn.SelectedImageIndex = ImgList.Images.IndexOfKey(strLX.Trim());
            }
        }
        #endregion

         #region 加载文件夹到树 

        /// <summary>
        /// 加载文件夹
        /// </summary>
        /// <param name="TV">TreeView</param>
        /// <param name="DirPath">要加载的文件夹路径</param>
        /// <param name="ImgList">ImageList</param>
        /// <returns></returns>
        public bool LoadDirectories(TreeView TV, string DirPath,ImageList ImgList)
        {
            try
            {
                TV.Nodes.Clear();
                TV.ImageList = ImgList;
                foreach (DirectoryInfo Dir in new DirectoryInfo(DirPath).GetDirectories())
                {
                    TreeNode TN = new TreeNode();
                    TN.Text = Dir.Name;
                    TN.Tag = Dir.FullName;
                    SetTreeNodeIcon(TN, "Dir", ImgList);
                    TV.Nodes.Add(TN);

                    ForLoadDirs(TN, Dir, ImgList);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 递归文件夹
        /// </summary>
        /// <param name="Tn"></param>
        /// <param name="Dir"></param>
        /// <param name="ImgList"></param>
        private void ForLoadDirs(TreeNode Tn, DirectoryInfo Dir, ImageList ImgList)
        {
            foreach (DirectoryInfo directory in Dir.GetDirectories())
            {
                TreeNode t = new TreeNode();
                t.Text = directory.Name;
                t.Tag = directory.FullName;
                SetTreeNodeIcon(Tn, "dir", ImgList);
                Tn.Nodes.Add(t);
                ForLoadDirs(t, directory, ImgList);
            }
        }
        #endregion

你可能感兴趣的:(treeview)