-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChooseFolderWindow.xaml.cs
More file actions
236 lines (210 loc) · 9.45 KB
/
ChooseFolderWindow.xaml.cs
File metadata and controls
236 lines (210 loc) · 9.45 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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.Shapes;
namespace LAVtechQuickRecover
{
/// <summary>
/// Interaction logic for ChooseFolderWindow.xaml
/// </summary>
public partial class ChooseFolderWindow : Window
{
public string SelectedFolder { get; private set; }
public ChooseFolderWindow(string winTitle = "choose folder")
{
InitializeComponent();
this.Title = winTitle;
//holdoff on this point for now.
//PopulateInputTreeViewAsync(); //it works but proves to be very inefficient.
}
//As seen with the code in comments below: the aim was to allow user to search for a directory, adn then select it. -> sadly my code is very inefficient, even moving it to become Async didn't resolve the issue
// current stance: either migrate to Ookii dialogs- or just work around it.
// will pick up here when there is a query to do so, so far, my best time is spent on building other projects*
}
}
/*
private async Task PopulateInputTreeViewAsync()
{
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
TreeViewItem item = new TreeViewItem
{
Header = drive.Name,
Tag = drive.RootDirectory.FullName
};
item.Expanded += FolderTreeViewItem_Expanded; // Hook the Expanded event
SelectFolderTreeView.Items.Add(item);
await PopulateSubdirectoriesAsync(item, 1);
}
}
private async Task PopulateSubdirectoriesAsync(TreeViewItem parentItem, int levelsToIndex)
{
try
{
DirectoryInfo parentDirInfo = new DirectoryInfo(parentItem.Tag.ToString());
DirectoryInfo[] subdirectories = parentDirInfo.GetDirectories();
foreach (DirectoryInfo subdirectory in subdirectories)
{
// Exclude system and hidden folders
if ((subdirectory.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0)
{
TreeViewItem subItem = new TreeViewItem
{
Header = subdirectory.Name,
Tag = subdirectory.FullName
};
subItem.Expanded += FolderTreeViewItem_Expanded; // Hook the Expanded event for subitems
parentItem.Items.Add(subItem);
if (levelsToIndex > 0)
{
// Index one additional level of subdirectories for the current node
await PopulateSubdirectoriesAsync(subItem, levelsToIndex - 1);
}
}
}
}
catch (UnauthorizedAccessException)
{
// Handle unauthorized access if needed
}
}
private async Task PopulateSubdirectories(TreeViewItem parentItem, int levelsToIndex)
{
try
{
DirectoryInfo parentDirInfo = new DirectoryInfo(parentItem.Tag.ToString());
DirectoryInfo[] subdirectories = parentDirInfo.GetDirectories();
foreach (DirectoryInfo subdirectory in subdirectories)
{
// Exclude system and hidden folders
if ((subdirectory.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0)
{
TreeViewItem subItem = new TreeViewItem
{
Header = subdirectory.Name,
Tag = subdirectory.FullName
};
subItem.Expanded += FolderTreeViewItem_Expanded; // Hook the Expanded event for subitems
parentItem.Items.Add(subItem);
if (levelsToIndex > 0)
{
// Index one additional level of subdirectories for the current node
await PopulateSubdirectories(subItem, levelsToIndex - 1);
}
}
}
}
catch (UnauthorizedAccessException)
{
// Handle unauthorized access if needed
}
}
private async void ChooseFolderWindow_Loaded(object sender, RoutedEventArgs e)
{
await PopulateInputTreeViewAsync();
}
private void FolderTreeViewItem_Expanded(object sender, RoutedEventArgs e)
{
if (sender is TreeViewItem expandedItem && expandedItem.Tag is string directory)
{
if (expandedItem.Items.Count == 1 && expandedItem.Items[0] is string dummyItem)
{
expandedItem.Items.Clear(); // Remove the dummy item if present
try
{
DirectoryInfo parentDirInfo = new DirectoryInfo(directory);
DirectoryInfo[] subdirectories = parentDirInfo.GetDirectories();
foreach (DirectoryInfo subdirectory in subdirectories)
{
// Exclude system and hidden folders
if ((subdirectory.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0)
{
TreeViewItem subItem = new TreeViewItem
{
Header = subdirectory.Name,
Tag = subdirectory.FullName
};
subItem.Items.Add("Loading..."); // Add a dummy item to indicate that it's loading
subItem.Expanded += SubFolderTreeViewItem_Expanded;
expandedItem.Items.Add(subItem);
}
}
}
catch (UnauthorizedAccessException)
{
// Handle unauthorized access if needed
}
}
}
}
private void SubFolderTreeViewItem_Expanded(object sender, RoutedEventArgs e)
{
if (sender is TreeViewItem expandedItem && expandedItem.Tag is string directory)
{
if (expandedItem.Items.Count == 1 && expandedItem.Items[0] is string dummyItem)
{
expandedItem.Items.Clear(); // Remove the dummy item if present
expandedItem.Items.Add("Loading...");
LoadSubdirectoriesAsync(expandedItem);
}
}
}
private async void LoadSubdirectoriesAsync(TreeViewItem parentItem)
{
try
{
if (parentItem.Tag is string directory)
{
DirectoryInfo parentDirInfo = new DirectoryInfo(directory);
DirectoryInfo[] subdirectories = parentDirInfo.GetDirectories();
int currentLevel = (int)parentItem.GetValue(LevelProperty);
currentLevel--; // Decrement the current level
foreach (DirectoryInfo subdirectory in subdirectories)
{
// Exclude system and hidden folders
if ((subdirectory.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0)
{
await Application.Current.Dispatcher.InvokeAsync(() =>
{
TreeViewItem subItem = new TreeViewItem
{
Header = subdirectory.Name,
Tag = subdirectory.FullName
};
if (currentLevel > 0)
{
subItem.SetValue(LevelProperty, currentLevel); // Set the current level for the subitem
subItem.Items.Add("Loading..."); // Add a dummy item to indicate that it's loading
subItem.Expanded += SubFolderTreeViewItem_Expanded;
}
parentItem.Items.Add(subItem);
});
}
}
}
}
catch (UnauthorizedAccessException)
{
// Handle unauthorized access if needed
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (SelectFolderTreeView.SelectedItem is TreeViewItem selectedItem && selectedItem.Tag is string selectedFolder)
{
SelectedFolder = selectedFolder;
Console.WriteLine("selected: " + SelectedFolder);
DialogResult = true;
}
}
*/