From 491c26e0d6d882b86f9c8582cebea99484958495 Mon Sep 17 00:00:00 2001 From: dahall Date: Mon, 1 Jun 2020 10:43:51 -0600 Subject: [PATCH] BREAKING CHANGE: Changed default behavior of ShellItem.Properties so that it is read-only. It was failing for all shell items that didn't have their own properties. To get previous behavior, simply set "shellItem.Properties.ReadOnly = false" before setting properties. --- UnitTests/Windows.Shell/ShellItemPropStoreTests.cs | 23 ++++++++++++++++++++-- Windows.Shell/ShellObjects/ShellItem.cs | 17 +++++++++++----- Windows.Shell/Vanara.Windows.Shell.csproj | 1 + 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/UnitTests/Windows.Shell/ShellItemPropStoreTests.cs b/UnitTests/Windows.Shell/ShellItemPropStoreTests.cs index c7228f01..ada50ddd 100644 --- a/UnitTests/Windows.Shell/ShellItemPropStoreTests.cs +++ b/UnitTests/Windows.Shell/ShellItemPropStoreTests.cs @@ -3,6 +3,9 @@ using System; using System.Collections; using System.Diagnostics; using System.Linq; +using System.Runtime.InteropServices; +using Vanara.PInvoke.Tests; +using static Vanara.PInvoke.Ole32; namespace Vanara.Windows.Shell.Tests { @@ -14,9 +17,9 @@ namespace Vanara.Windows.Shell.Tests [Test] public void DescriptionTest() { - using var i = new ShellItem(testDoc); + using var i = new ShellItem(TestCaseSources.LogFile); var c = 0; - foreach (var d in i.Properties.Descriptions) + foreach (var d in i.PropertyDescriptions) { c++; Assert.That(d, Is.Not.Null); @@ -75,6 +78,22 @@ namespace Vanara.Windows.Shell.Tests Assert.That(c, Is.EqualTo(i.Properties.Count)); } + [Test] + public void WritableTest() + { + // Try accessing a text file's writable properties and assert a failure. + using (var i = new ShellItem(TestCaseSources.LogFile)) + { + i.Properties.ReadOnly = false; + Assert.That(() => i.Properties.TryGetValue(PROPERTYKEY.System.Size, out var val), Throws.TypeOf()); + } + + // Try accessing a Word file's writable properties and assert successs. + using var w = new ShellItem(testDoc); + w.Properties.ReadOnly = false; + Assert.That(w.Properties.TryGetValue(PROPERTYKEY.System.Size, out _), Is.True); + } + [Test] public void ShellItemPropertyStoreTest1() { diff --git a/Windows.Shell/ShellObjects/ShellItem.cs b/Windows.Shell/ShellObjects/ShellItem.cs index 52bdf57a..5fd579bd 100644 --- a/Windows.Shell/ShellObjects/ShellItem.cs +++ b/Windows.Shell/ShellObjects/ShellItem.cs @@ -6,7 +6,6 @@ using System.Drawing; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Text; -using System.Threading; using Vanara.Extensions; using Vanara.InteropServices; using Vanara.PInvoke; @@ -458,13 +457,21 @@ namespace Vanara.Windows.Shell } } - /// Gets the property store for the item. + /// + /// Gets the property store for the item. + /// Initially, this property store is the read-only store (change from R/W in v3.2.9) and should always have properties. + /// However, setting any of the properties of this value change the function of all subsequent uses. For example, if you set the + /// value to , all subsequent calls to will access the read-write property store. If this does not support + /// properties directly, your use of this property will fail. It is important that you check for exceptions when changing the + /// properties of this value to prevent unexpected failures. + /// /// The dictionary of properties. - public ShellItemPropertyStore Properties => props ?? (props = new ShellItemPropertyStore(this) { ReadOnly = false }); + public ShellItemPropertyStore Properties => props ??= new ShellItemPropertyStore(this); - /// Gets a property description list object containing descriptions of all properties. + /// Gets a property description list object containing descriptions of all properties suitable to be shown in the UI. /// A complete instance. - public PropertyDescriptionList PropertyDescriptions => propDescList ?? (propDescList = GetPropertyDescriptionList(PROPERTYKEY.System.PropList.FullDetails)); + public PropertyDescriptionList PropertyDescriptions => propDescList ??= GetPropertyDescriptionList(PROPERTYKEY.System.PropList.FullDetails); /// Gets the normal tool tip text associated with this item. /// The tool tip text. diff --git a/Windows.Shell/Vanara.Windows.Shell.csproj b/Windows.Shell/Vanara.Windows.Shell.csproj index f320a503..086276ee 100644 --- a/Windows.Shell/Vanara.Windows.Shell.csproj +++ b/Windows.Shell/Vanara.Windows.Shell.csproj @@ -17,6 +17,7 @@ Enumerations ChangeFilters, ExecutableType, FolderItemFilter, LibraryFolderFilter, LibraryViewTemplate, LinkResolution, OperationFlags, ShellIconType, ShellImageSize, ShellItemAttribute, ShellItemComparison, ShellItemDisplayString, ShellItemGetImageOptions, ShellItemToolTipOptions, TaskbarButtonProgressState, TaskbarItemTabThumbnailOption, TransferFlags, VerbMultiSelectModel, VerbPosition, VerbSelectionModel, Visibility + Vanara.Windows.Shell