WPF-TreeView遍历硬盘所有目录
首先在winform中做出了基本的界面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.IO; 9 using System.Threading; 10 namespace TViewSource 11 { 12 public partial class MainForm2 : Form 13 { 14 public MainForm2() 15 { 16 InitializeComponent(); 17 this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 18 } 19 20 private void MainForm2_Load(object sender, EventArgs e) 21 { 22 BindDrives(); 23 } 24 25 private void BindDrives() 26 { 27 DriveInfo[] drvs = DriveInfo.GetDrives(); 28 foreach (DriveInfo drv in drvs) 29 { 30 TreeNode root = new TreeNode(); 31 root.Text = drv.Name; 32 root.Tag = drv.RootDirectory.ToString(); 33 // root.Nodes.Add(""); 34 treeView1.Nodes.Add(root); 35 if (Directory.Exists(drv.RootDirectory.ToString())) 36 { 37 DirectoryInfo dInfo = new DirectoryInfo(drv.RootDirectory.ToString()); 38 39 FileSystemInfo[] files = dInfo.GetFileSystemInfos(); 40 if (files.Length > 0) //有子节点,先添加一个空节点 41 { 42 root.Nodes.Add("emptyNode", string.Empty); 43 } 44 } 45 } 46 } 47 48 //展开节点,移除以前的空节点,加载子节点 49 private void treeView1_AfterExpand(object sender, TreeViewEventArgs e) 50 { 51 52 TreeNode parentNode = e.Node; 53 // parentNode.Nodes.RemoveByKey("emptyNode");//移除空节点 54 parentNode.Nodes.Clear(); 55 string path = parentNode.Tag.ToString(); 56 57 if (Directory.Exists(path)) 58 { 59 DirectoryInfo dir = new DirectoryInfo(path); 60 61 FileSystemInfo[] files = dir.GetFileSystemInfos(); 62 foreach (FileSystemInfo f in files) 63 { 64 TreeNode node = new TreeNode(); 65 node.Text = f.Name; 66 node.Tag = f.FullName; 67 parentNode.Nodes.Add(node); //加载子节点 68 69 if (Directory.Exists(node.Tag.ToString())) 70 { 71 DirectoryInfo subDir = new DirectoryInfo(node.Tag.ToString()); 72 if (subDir.Attributes != (FileAttributes.System | FileAttributes.Hidden | FileAttributes.Directory)) 73 { 74 FileSystemInfo[] subFiles = subDir.GetFileSystemInfos(); 75 if (subFiles.Length > 0) //有子节点,先添加一个空节点 76 { 77 node.Nodes.Add("emptyNode", string.Empty); 78 } 79 } 80 } 81 } 82 83 } 84 85 86 } 87 } 88 }
在WPF中的结构:
代码分别是:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 * 由SharpDevelop创建。 3 * 用户: LBQ 4 * 日期: 2015/8/3 5 * 时间: 17:24 6 * 7 * 8 */ 9 using System; 10 using System.Collections.Generic; 11 using System.IO; 12 namespace t 13 { 14 15 class BindDirectory 16 { 17 public BindDirectory(string directoryPath) 18 { 19 //正规化目录路径,确保Path以'\\'结尾 20 directoryPath = directoryPath.TrimEnd('\\'); 21 Path = directoryPath + '\\'; 22 //计算出目录名称(不包含路径) 23 int indexLastSlash = directoryPath. 24 LastIndexOf('\\'); 25 if (indexLastSlash >= 0) 26 { 27 Name = directoryPath.Substring 28 (indexLastSlash + 1); 29 } 30 else 31 { 32 Name = directoryPath; 33 } 34 } 35 public string Name 36 { 37 get; 38 private set; 39 } 40 public string Path 41 { 42 get; 43 private set; 44 } 45 public IEnumerable<BindDirectory> 46 Directories 47 { 48 get 49 { 50 //延迟加载 51 if (directories == null) 52 { 53 directories = new List 54 <BindDirectory>(); 55 foreach (string d in Directory. 56 GetDirectories(Path)) 57 { 58 directories.Add(new 59 BindDirectory(d)); 60 } 61 } 62 return directories; 63 } 64 } 65 List<BindDirectory> directories; 66 } 67 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 * 由SharpDevelop创建。 3 * 用户: LBQ 4 * 日期: 2015/8/3 5 * 时间: 17:27 6 * 7 * 8 */ 9 using System; 10 using System.Collections.Generic; 11 using System.IO; 12 13 namespace t 14 { 15 class BindDirectoryList : List<BindDirectory> 16 { 17 public BindDirectoryList() 18 { 19 foreach (var drive in DriveInfo.GetDrives()) 20 { 21 Add(new BindDirectory(drive.RootDirectory.FullName)); 22 } 23 } 24 } 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Window x:Class="t.Window1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:t" 7 xmlns:sample="clr-namespace:t" 8 mc:Ignorable="d" 9 Title="Window1" Height="300" Width="300"> 10 <Window.Resources> 11 <!--引入我们自己的数据提供对象--> 12 <ObjectDataProvider x:Key="drives" ObjectType="{x:Type sample:BindDirectoryList}"/> 13 <!--设置如何显示数据,以及如何获 14 取下一级数据的列表--> 15 <HierarchicalDataTemplate x:Key="itemTemplate" DataType="{x:Type sample:BindDirectory}" ItemsSource="{Binding Directories}"> 16 <TextBlock Text="{Binding Name}"/> 17 </HierarchicalDataTemplate> 18 </Window.Resources> 19 <Grid> 20 <TreeView Background="AliceBlue" ItemsSource="{Binding Source={StaticResource drives}}" 21 ItemTemplate="{StaticResource itemTemplate}" > 22 </TreeView> 23 </Grid> 24 </Window>