-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
211 lines (163 loc) · 5.46 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Path = System.IO.Path;
namespace TreeView
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Constructor
/// <summary>
/// Default constructor
/// </summary>
public MainWindow()
{
InitializeComponent();
var t = new TreeViewItem();
}
#endregion
#region On Loaded
/// <summary>
/// When the application first opens
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
// Get every logical drive on the machine
foreach (var drive in Directory.GetLogicalDrives())
{
// Create a new item for it
var item = new TreeViewItem()
{
// Set the header for it
Header = drive,
// Amd the full path
Tag = drive
};
// Add a dummy item
item.Items.Add(null);
// Listen out for item being expanded
item.Expanded += Folder_Expanded;
// Add it to the main tree-view
FolderView.Items.Add(item);
}
}
#endregion
#region Folder_Expanded method
/// <summary>
/// When a folder is expanded, find the sub folders/files.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Folder_Expanded(object sender, RoutedEventArgs e)
{
#region Inital checks
var item = (TreeViewItem)sender;
// If the item only contains the dummy data
if (item.Items.Count != 1 || item.Items[0] != null)
return;
// Clear dummy data
item.Items.Clear();
// Get full path
var fullPath = (string)item.Tag;
#endregion
#region Get Folders
// Create a blank list for directories
var directories = new List<string>();
// Try and get directories from the folder
// ignoring any issues doing so
try
{
var dirs = Directory.GetDirectories(fullPath);
if (dirs.Length > 0)
directories.AddRange(dirs);
}
catch { }
// For each directory
directories.ForEach(directoryPath =>
{
// Create directory item
var subItem = new TreeViewItem()
{
// set header as folder name
Header = GetFileFolderName(directoryPath),
// and tag as full path
Tag = directoryPath
};
// Add dummy item so we can expand folder
subItem.Items.Add(null);
// Handle expanding
subItem.Expanded += Folder_Expanded;
// Add this item to the parent
item.Items.Add(subItem);
});
#endregion
#region Get Files
var files = new List<string>();
// Try and get directories from the folder
// ignoring any issues doing so
try
{
var fs = Directory.GetFiles(fullPath);
if (fs.Length > 0)
files.AddRange(fs);
}
catch { }
// For each file
files.ForEach(filePath =>
{
// Create file item
var subItem = new TreeViewItem()
{
// set header as file name
Header = GetFileFolderName(filePath),
// and tag as full path
Tag = filePath
};
// Add this item to the parent
item.Items.Add(subItem);
});
#endregion
}
#endregion
#region Helpers
/// <summary>
/// Find the file name from a full path
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public static string GetFileFolderName(string path)
{
// If we have no path, return empty
if (string.IsNullOrEmpty(path))
return string.Empty;
// make all slashes back slashes
var normalizedPath = path.Replace('/', '\\');
// Find the last backslash in the path
var lastIndex = normalizedPath.LastIndexOf('\\');
// If we dont find a backslash, return the path itself
if (lastIndex <= 0)
return path;
// Return the name after the last back slash
return path.Substring(lastIndex + 1);
}
#endregion
}
}