Repository navigation
Expand file tree
/
Copy pathWnetResourceList.cs
More file actions
515 lines (444 loc) · 16.9 KB
/
Copy pathWnetResourceList.cs
File metadata and controls
515 lines (444 loc) · 16.9 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
using System;
using System.Collections.Generic;
using netCommander.WNet;
using netCommander.NetApi;
using System.ComponentModel;
namespace netCommander
{
public class WnetResourceList : FileCollectionBase
{
private readonly string SORT_NAME = "Name";
private SortedList<NETRESOURCE, object> internal_list = null;
private IComparer<NETRESOURCE> internal_comparer = null;
private NETRESOURCE resource_root = new NETRESOURCE();
private InternalCache parent_cache = null;
public WnetResourceList(int sort_criteria, bool reverse)
: base(sort_criteria, reverse)
{
//root resouces
resource_root = WinApiWNETwrapper.RootNetresource;
internal_comparer = new InternalComparer(sort_criteria, reverse);
internal_list = new SortedList<NETRESOURCE, object>(internal_comparer);
}
public WnetResourceList(int sort_criteria, bool reverse, NETRESOURCE root)
: base(sort_criteria, reverse)
{
resource_root = root;
internal_comparer = new InternalComparer(sort_criteria, reverse);
internal_list = new SortedList<NETRESOURCE, object>(internal_comparer);
}
/// <summary>
/// Use try with this ctor.
/// </summary>
/// <param name="sort_criteria"></param>
/// <param name="reverse"></param>
/// <param name="net_path"></param>
public WnetResourceList(int sort_criteria, bool reverse, string net_path)
: base(sort_criteria, reverse)
{
resource_root = WinApiWNETwrapper.GetResourceInfo(net_path);
internal_comparer = new InternalComparer(sort_criteria, reverse);
internal_list = new SortedList<NETRESOURCE, object>(internal_comparer);
}
private bool establish_connection(NETRESOURCE target_resource)
{
//ask creds
string user_name = string.Empty;
string ps = string.Empty;
if (Messages.AskCredentials
("Network login",
"Connect to " + target_resource.lpRemoteName,
ref user_name,
ref ps) != System.Windows.Forms.DialogResult.OK)
{
return false;
}
int res = WinApiWNET.WNetAddConnection2
(ref target_resource,
ps,
user_name,
WNetConnectOptions.None);
if (res != WinApiWNET.NO_ERROR)
{
Win32Exception win_ex = new Win32Exception(res);
Messages.ShowException(win_ex);
return false;
}
return true;
}
private NETRESOURCE[] get_hidden_resources(NETRESOURCE res)
{
List<NETRESOURCE> ret = new List<NETRESOURCE>();
try
{
string server_name = res.lpRemoteName.TrimStart(new char[] { '\\' });
SHARE_INFO_1[] net_shares = WinApiNETwrapper.GetShareInfos_1(server_name);
foreach (SHARE_INFO_1 info in net_shares)
{
if (info.shi1_netname.EndsWith("$"))
{
NETRESOURCE hid_res = new NETRESOURCE();
if ((info.shi1_type & NetshareType.DEVICE) == NetshareType.DEVICE)
{
//IPC$
hid_res.dwDisplayType = ResourceDisplayType.GENERIC;
hid_res.dwType = ResourceType.ANY;
}
else if ((info.shi1_type & NetshareType.PRINTQ) == NetshareType.PRINTQ)
{
hid_res.dwDisplayType = ResourceDisplayType.SHAREADMIN;
hid_res.dwType = ResourceType.PRINT;
}
else if ((info.shi1_type & NetshareType.SPECIAL) == NetshareType.SPECIAL)
{
//C$
hid_res.dwDisplayType = ResourceDisplayType.SHAREADMIN;
hid_res.dwType = ResourceType.DISK;
}
hid_res.lpComment = info.shi1_remark;
hid_res.lpProvider = res.lpProvider;
hid_res.lpRemoteName = string.Format(@"{0}\{1}", res.lpRemoteName, info.shi1_netname);
ret.Add(hid_res);
}
}
}
catch (Exception) { }
return ret.ToArray();
}
protected override void internal_dispose()
{
//nothing to do
}
protected override void internal_sort(int criteria_index, bool reverse_order)
{
InternalComparer new_comparer = new InternalComparer(criteria_index, reverse_order);
SortedList<NETRESOURCE, object> new_list = new SortedList<NETRESOURCE, object>(new_comparer);
foreach (KeyValuePair<NETRESOURCE, object> kvp in internal_list)
{
new_list.Add(kvp.Key, kvp.Value);
}
internal_comparer = new_comparer;
internal_list = new_list;
}
protected override void internal_refill()
{
List<NETRESOURCE> new_list = new List<NETRESOURCE>();
//create enumerator
WnetResourceEnumerator res_enum = null;
try
{
OnLongOperaion("Wait while scanning network...", true);
res_enum = new WnetResourceEnumerator
(resource_root,
ResourceScope.GLOBALNET,
ResourceType.ANY,
ResourceUsage.NONE);
OnLongOperaion("Scanning completed.", false);
}
catch (Win32Exception win_ex)
{
if (win_ex.NativeErrorCode == 5) //access denied
{
//try to establish connection with net credetials
if (establish_connection(resource_root))
{
//retry
res_enum = new WnetResourceEnumerator
(resource_root,
ResourceScope.GLOBALNET,
ResourceType.ANY,
ResourceUsage.NONE);
}
else
{
throw win_ex;
}
}
else
{
throw win_ex;
}
}
finally
{
OnLongOperaion("Completed", false);
}
//fill
foreach (NETRESOURCE one_res in res_enum)
{
new_list.Add(one_res);
}
//case if resourse_root is SMB server - add hidden shares
if ((resource_root.dwDisplayType == ResourceDisplayType.SERVER) &&
(WinApiWNETwrapper.GetNetworkInfo(resource_root.lpProvider).wNetType == NetworkType.LANMAN))
{
//retrieve fake hidden NETRESOURCEs
OnLongOperaion("Wait while scanning hidden resources...", true);
NETRESOURCE[] hidden_resources = get_hidden_resources(resource_root);
OnLongOperaion("Scanning completed.", false);
new_list.AddRange(hidden_resources);
}
//fill work list
internal_list.Clear();
foreach (NETRESOURCE one_res in new_list)
{
internal_list.Add(one_res, null);
}
}
public override string[] SortCriteriaAvailable
{
get { return new string[] { SORT_NAME }; }
}
public override string GetItemDisplayName(int index)
{
string ret = string.Empty;
if (index == 0)
{
ret = ".."; //ref to root
}
else
{
ret = string.Format
("{0}",
internal_list.Keys[index - 1].lpRemoteName);
}
return ret;
}
public override string GetItemDisplayNameLong(int index)
{
string ret = string.Empty;
if (index == 0)
{
ret = "<UP>";
}
else
{
ret = GetItemDisplayName(index);
}
return ret;
}
public override string GetItemDisplaySummaryInfo(int index)
{
NETRESOURCE res = new NETRESOURCE();
string ret = string.Empty;
res = index == 0 ? resource_root : internal_list.Keys[index - 1];
ret = string.Format
("{0} {1}",
res.dwDisplayType.ToString(),
res.lpComment);
return ret;
}
public override string GetSummaryInfo()
{
return string.Format
("{0} entry(s) [{1}]",
ItemCount - 1,
resource_root.lpRemoteName);
}
public override string GetSummaryInfo(int[] indices)
{
return GetSummaryInfo();
}
public override int ItemCount
{
get { return internal_list.Count + 1; }
}
public override bool GetItemSelectEnable(int index)
{
return false;
}
public override bool GetItemIsContainer(int index)
{
if (index == 0)
{
return true;
}
else
{
return
((internal_list.Keys[index - 1].dwUsage & ResourceUsage.CONTAINER) ==
ResourceUsage.CONTAINER) ||
(internal_list.Keys[index - 1].dwType == ResourceType.DISK);
}
}
public override void GetChildCollection
(int index,
ref FileCollectionBase new_collection,
ref bool use_new,
ref string preferred_focused_text)
{
if (!GetItemIsContainer(index))
{
use_new = false;
return;
}
NETRESOURCE target_container = new NETRESOURCE();
if (index == 0)
{
//go up to container of resourse_root
//cache valid?
if (parent_cache != null)
{
//fill from cache
internal_list.Clear();
foreach (NETRESOURCE res in parent_cache.ResourceList)
{
internal_list.Add(res, null);
}
preferred_focused_text = resource_root.lpRemoteName;
resource_root = parent_cache.ResourceRoot;
parent_cache = parent_cache.ParentCache;
OnAfterRefill();
use_new = false;
return;
}
//workaround.
//Wnet throws exception while getting parent of resource like 'Microsoft Windows Network'
if ((resource_root.lpRemoteName == null) ||
(resource_root.dwDisplayType == ResourceDisplayType.NETWORK))
{
target_container = WinApiWNETwrapper.RootNetresource;
}
else
{
target_container = WinApiWNETwrapper.GetParentResource(resource_root);
}
preferred_focused_text = resource_root.lpRemoteName;
//set new resource_root
resource_root = target_container;
//and fill
Refill();
//and return
use_new = false;
return;
}//end of UP brunch
//else go to down
target_container = internal_list.Keys[index - 1];
//case disk share - switch to DirectoryList
if (target_container.dwType == ResourceType.DISK)
{
//switch to DirectoryList
DirectoryList new_source = new DirectoryList
(SortCriteria, SortReverse, target_container.lpRemoteName);
new_source.MainWindow = MainWindow;
try
{
new_source.Refill();
use_new = true;
new_collection = new_source;
}
catch (Win32Exception win5_ex)
{
if (win5_ex.NativeErrorCode == 5) //access denied
{
//try to establish connection with creds
if (establish_connection(target_container))
{
try
{
//and retry
new_source.Refill();
use_new = true;
new_collection = new_source;
}
catch (Exception ex)
{
Messages.ShowException(ex);
}
}
}
else
{
Messages.ShowException(win5_ex);
}
}
catch (Exception ex)
{
Messages.ShowException(ex);
}
return;
}
//prepare parent cache
InternalCache new_cache = new InternalCache(target_container);
new_cache.ParentCache = parent_cache;
new_cache.ResourceList.AddRange(internal_list.Keys);
new_cache.ResourceRoot = resource_root;
try
{
resource_root = target_container;
use_new = false;
Refill();
parent_cache = new_cache;
}
catch (Exception ex)
{
Messages.ShowException(ex);
}
return;
}
public override int FindIndexOfName(string name)
{
int ret = -1;
for (int i = 0; i < internal_list.Count; i++)
{
if (internal_list.Keys[i].lpRemoteName == name)
{
ret = i + 1;
break;
}
}
return ret;
}
public override int[] FindItems()
{
throw new NotImplementedException();
}
public override string GetStatusText()
{
return resource_root.lpRemoteName == null ? "Network" : resource_root.lpRemoteName;
}
#region cache
private class InternalCache
{
public List<NETRESOURCE> ResourceList { get; set; }
public InternalCache ParentCache { get; set; }
public NETRESOURCE ResourceRoot { get; set; }
public InternalCache(NETRESOURCE resource_root)
{
ResourceList = new List<NETRESOURCE>();
ParentCache = null;
ResourceRoot = resource_root;
}
}
#endregion
#region comparer
private class InternalComparer : IComparer<NETRESOURCE>
{
private int _order = 1;
private int sort_criteria = 0;
public InternalComparer(int sort_criteria, bool sort_reverse)
{
this.sort_criteria = sort_criteria;
_order = sort_reverse ? -1 : 1;
}
#region IComparer<NETRESOURCE> Members
public int Compare(NETRESOURCE x, NETRESOURCE y)
{
int ret = 0;
switch (sort_criteria)
{
case 0:
ret = string.Compare(x.lpRemoteName, y.lpRemoteName);
break;
default:
ret = string.Compare(x.lpRemoteName, y.lpRemoteName);
break;
}
ret = _order * ret;
return ret;
}
#endregion
}
#endregion
}
}