From a391f0c18373cdead49cf1c4c0658076e87c8a91 Mon Sep 17 00:00:00 2001 From: dahall Date: Thu, 9 Jun 2022 22:32:26 -0600 Subject: [PATCH] More work on ShellFolderCategorizer --- UnitTests/Windows.Shell/ShellFolderTests.cs | 10 ++++++++++ Windows.Shell.Common/ShellObjects/ShellFolder.cs | 3 ++- Windows.Shell.Common/ShellObjects/ShellFolderCategorizer.cs | 13 +++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/UnitTests/Windows.Shell/ShellFolderTests.cs b/UnitTests/Windows.Shell/ShellFolderTests.cs index a7d8c012..0d57bb3e 100644 --- a/UnitTests/Windows.Shell/ShellFolderTests.cs +++ b/UnitTests/Windows.Shell/ShellFolderTests.cs @@ -137,5 +137,15 @@ namespace Vanara.Windows.Shell.Tests Assert.That(() => f.GetChildrenUIObjects(default, i), Throws.TypeOf()); Assert.That(() => f.GetViewObject(default), Throws.TypeOf()); } + + [Test] + public void CategoryTest() + { + using var ie = new ShellFolder(KNOWNFOLDERID.FOLDERID_Documents); + Assert.That(ie.Categories, Is.Not.Empty); + Assert.That(ie.Categories.DefaultCategory?.Name, Is.Not.Null); + foreach (var c in ie.Categories) + TestContext.WriteLine($"{c.Name}"); + } } } \ No newline at end of file diff --git a/Windows.Shell.Common/ShellObjects/ShellFolder.cs b/Windows.Shell.Common/ShellObjects/ShellFolder.cs index e6418bd3..2107968b 100644 --- a/Windows.Shell.Common/ShellObjects/ShellFolder.cs +++ b/Windows.Shell.Common/ShellObjects/ShellFolder.cs @@ -56,6 +56,7 @@ namespace Vanara.Windows.Shell public class ShellFolder : ShellItem, IEnumerable { internal IShellFolder iShellFolder; + private ShellFolderCategorizer categories; private static ShellFolder desktop; /// Initializes a new instance of the class. @@ -177,7 +178,7 @@ namespace Vanara.Windows.Shell /// Gets the registered categorizers. /// The categorizers. - public ShellFolderCategorizer Categories => new(IShellFolder); + public ShellFolderCategorizer Categories => categories ??= new(IShellFolder); /// /// Enumerates all children of this item. If this item is not a folder/container, this method will return an empty enumeration. diff --git a/Windows.Shell.Common/ShellObjects/ShellFolderCategorizer.cs b/Windows.Shell.Common/ShellObjects/ShellFolderCategorizer.cs index bcca0cc7..4377c43c 100644 --- a/Windows.Shell.Common/ShellObjects/ShellFolderCategorizer.cs +++ b/Windows.Shell.Common/ShellObjects/ShellFolderCategorizer.cs @@ -21,8 +21,12 @@ public class ShellFolderCategorizer : IEnumerable { get { - ICategoryProvider.GetDefaultCategory(out var guid, out _).ThrowIfFailed(); - return GetCat(guid); + var hr = ICategoryProvider.GetDefaultCategory(out var guid, out _); + if (hr == HRESULT.S_OK) + return GetCat(guid); + if (hr == HRESULT.S_FALSE) + return null; + throw hr.GetException(); } } @@ -43,8 +47,9 @@ public class ShellFolderCategorizer : IEnumerable /// public IEnumerator GetEnumerator() { - ICategoryProvider.EnumCategories(out IEnumGUID penum).ThrowIfFailed(); - return new IEnumFromCom(penum.Next, penum.Reset).Select(g => GetCat(g)).GetEnumerator(); + if (ICategoryProvider.EnumCategories(out IEnumGUID penum) == HRESULT.S_OK) + return new IEnumFromCom(penum.Next, penum.Reset).Select(g => GetCat(g)).GetEnumerator(); + return Enumerable.Empty().GetEnumerator(); } ///