-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIShellItemArrayExtensions.cs
More file actions
49 lines (44 loc) · 1.68 KB
/
Copy pathIShellItemArrayExtensions.cs
File metadata and controls
49 lines (44 loc) · 1.68 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
namespace ShellN.Extensions;
public static class IShellItemArrayExtensions
{
public static IEnumerable<ShellItem> Enumerate(this IComObject<IShellItemArray>? array, bool noFolder = false, bool owned = true, bool throwOnError = false) => Enumerate(array?.Object, noFolder, owned, throwOnError);
public static IEnumerable<ShellItem> Enumerate(this IShellItemArray? array, bool noFolder = false, bool owned = true, bool throwOnError = false)
{
if (array == null)
yield break;
array.GetCount(out var count).ThrowOnError(throwOnError);
for (uint i = 0; i < count; i++)
{
array.GetItemAt(i, out var item).ThrowOnError(throwOnError);
if (item != null)
{
var si = ShellItem.FromObject(item, noFolder, owned);
if (si != null)
yield return si;
}
}
}
public unsafe static IComObject<IShellItemArray>? ToShellItemArray(this IEnumerable<ShellItem> items, bool throwOnError = false)
{
ArgumentNullException.ThrowIfNull(items);
var idLists = new List<ItemIdList>();
var pidls = new List<nint>();
foreach (var item in items)
{
var idl = item.GetIdList()!;
idLists.Add(idl);
pidls.Add(idl.Pointer);
}
try
{
var hr = Functions.SHCreateShellItemArrayFromIDLists(pidls.Length(), [.. pidls], out var arrayObj).ThrowOnError(throwOnError);
if (hr.IsError)
return null;
return new ComObject<IShellItemArray>(arrayObj)!;
}
finally
{
idLists.Dispose();
}
}
}