728x90
반응형
WPF & C# - 폴더트리 코드 소스 ( FolderTree / TreeView / 재귀함수 / 루프 / roop ) |
MainWindow.xaml.cs
1 <TreeView x:Name="_TreeView"/>cs
MainWindow.xaml.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 private void Window_Loaded(object sender, RoutedEventArgs e){// foreach (string str in Directory.GetDirectories("")) // 특정폴더foreach (string str in Directory.GetLogicalDrives()) // 루트폴더{try{TreeViewItem item = new TreeViewItem();item.Header = str;item.Tag = str;item.Expanded += new RoutedEventHandler(item_Expanded); // 노드 확장시 추가_TreeView.Items.Add(item);GetSubDirectories(item);}catch (Exception except){// MessageBox.Show(except.Message); // 접근 거부 폴더로 인해 주석처리}}}// 서브 디렉토리private void GetSubDirectories(TreeViewItem itemParent){if (itemParent == null) return;if (itemParent.Items.Count != 0) return;try{string strPath = itemParent.Tag as string;DirectoryInfo dInfoParent = new DirectoryInfo(strPath);foreach (DirectoryInfo dInfo in dInfoParent.GetDirectories()){TreeViewItem item = new TreeViewItem();item.Header = dInfo.Name;item.Tag = dInfo.FullName;item.Expanded += new RoutedEventHandler(item_Expanded);itemParent.Items.Add(item);}}catch (Exception except){// MessageBox.Show(except.Message); // 접근 거부 폴더로 인해 주석처리}}// 트리확장시 내용 추가void item_Expanded(object sender, RoutedEventArgs e){TreeViewItem itemParent = sender as TreeViewItem;if (itemParent == null) return;if (itemParent.Items.Count == 0) return;foreach (TreeViewItem item in itemParent.Items){GetSubDirectories(item);}}cs
원문출처 : http://inasie.tistory.com/19
너무 잘 만들어 놓아서 사실 따로 정리할게 없다.
넘넘 감사합니다^^
728x90
반응형