winform treeview 绑定文件夹和文件
转载:http://www.cnblogs.com/zhbsh/archive/2011/05/26/2057733.html
#region treeview 绑定文件夹和文件 /// <summary> /// 根据文件夹绑定到树 /// </summary> /// <param name="treeview"></param> /// <param name="FilePath"></param> /// <returns></returns> public bool SetTreeNoByFilePath(TreeView treeview, string FilePath,ImageList imgs) { treeview.Nodes.Clear(); treeview.ImageList = imgs; try { foreach (DirectoryInfo direc in new DirectoryInfo(FilePath).GetDirectories()) { TreeNode tn = new TreeNode(direc.Name); tn.Text = direc.FullName; SetTreeNodeIco(tn, "dir",imgs); tn.Tag = direc.FullName; SetSubDirectoryTreenode(direc, tn,imgs); treeview.Nodes.Add(tn); } foreach (FileInfo finfo in new DirectoryInfo(FilePath).GetFiles()) { TreeNode temptreenode = new TreeNode(finfo.Name); temptreenode.Tag = finfo.FullName; temptreenode.Text = finfo.Name; SetTreeNodeIco(temptreenode, finfo.Extension, imgs); treeview.Nodes.Add(temptreenode); } return true; } catch { return false; } } /// <summary> /// 设置子目录的 /// </summary> /// <param name="direc">目录路径</param> /// <param name="tn"></param> /// <param name="imglist"></param> private void SetSubDirectoryTreenode(DirectoryInfo direc, TreeNode tn,ImageList imglist) { foreach (DirectoryInfo dir in new DirectoryInfo(direc.FullName).GetDirectories()) { TreeNode temptn = new TreeNode(dir.Name); temptn.Tag = dir.FullName; SetTreeNodeIco(temptn, "dir", imglist); temptn.Text = dir.Name; tn.Nodes.Add(temptn); foreach (FileInfo fileinfo in new DirectoryInfo(dir.FullName).GetFiles()) { TreeNode temptreenode = new TreeNode(fileinfo.Name); temptreenode.Tag = fileinfo.FullName; temptreenode.Text = fileinfo.Name; SetTreeNodeIco(temptreenode, fileinfo.Extension,imglist); temptn.Nodes.Add(temptreenode); } SetSubDirectoryTreenode(dir, temptn, imglist); } } /// <summary> /// 为treeview设置小图标 /// </summary> /// <param name="tn"></param> /// <param name="strExt"></param> /// <param name="imgs"></param> private void SetTreeNodeIco(TreeNode tn, string strExt, ImageList imgs) { string ext = strExt.Replace(".", ""); if (ext.ToLower() == "dir") { tn.ImageIndex = imgs.Images.IndexOfKey("close"); tn.SelectedImageIndex = imgs.Images.IndexOfKey("open"); } else if (ext.ToLower() == "doc" || ext.ToLower() == "rar" || ext.ToLower() == "txt") { tn.ImageIndex = imgs.Images.IndexOfKey(ext); tn.SelectedImageIndex = imgs.Images.IndexOfKey(ext); } else { tn.ImageIndex = imgs.Images.IndexOfKey("other"); tn.SelectedImageIndex = imgs.Images.IndexOfKey("other"); } } #endregion #region 只绑定文件夹 /// <summary> /// 根据文件夹绑定到树 /// </summary> /// <param name="treeview"></param> /// <param name="FilePath"></param> /// <returns></returns> public bool SetTreeNoByFilePath(TreeView treeview, string FilePath) { treeview.Nodes.Clear(); try { foreach (DirectoryInfo direc in new DirectoryInfo(FilePath).GetDirectories()) { TreeNode tn = new TreeNode(direc.Name); tn.Text = direc.FullName; tn.Tag = direc.FullName; SetSubDirectoryTreenode(direc, tn); treeview.Nodes.Add(tn); } return true; } catch { return false; } } /// <summary> /// 设置子目录的 /// </summary> /// <param name="direc">目录路径</param> /// <param name="tn"></param> /// <param name="imglist"></param> private void SetSubDirectoryTreenode(DirectoryInfo direc, TreeNode tn) { foreach (DirectoryInfo dir in new DirectoryInfo(direc.FullName).GetDirectories()) { TreeNode temptn = new TreeNode(dir.Name); temptn.Tag = dir.FullName; temptn.Text = dir.Name; tn.Nodes.Add(temptn); foreach (FileInfo fileinfo in new DirectoryInfo(dir.FullName).GetFiles()) { TreeNode temptreenode = new TreeNode(fileinfo.Name); temptreenode.Tag = fileinfo.FullName; temptreenode.Text = fileinfo.Name; temptn.Nodes.Add(temptreenode); } SetSubDirectoryTreenode(dir, temptn); } } #endregion
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET