diff --git a/Changelog.md b/Changelog.md index da05365d..0681d08c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,14 @@ # Changelog +13/05/2026 + +Version 8.0.15.2 + +* Fixed [#47](https://github.com/PWagner1/Windows-API-CodePack-NET/pull/47), `WindowsAPICodePackSensors` throws a `NullReferenceException` when enumerating ambient light sensors (thanks to [xaviergonz](https://github.com/xaviergonz)) +* Fixed file & NuGet package version alignment + +========= + 02/04/2026 Version 8.0.15.1 diff --git a/Directory.Build.props b/Directory.Build.props index 121bdbe8..5057db7a 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 8.0.17 - 8.0.17 + 8.0.15.2 + 8.0.15.2 \ No newline at end of file diff --git a/Source/Current/Windows API CodePack/Components/Core/AppRestartRecovery/RecoveryData.cs b/Source/Current/Windows API CodePack/Components/Core/AppRestartRecovery/RecoveryData.cs index ba6df2a5..a1eaa096 100644 --- a/Source/Current/Windows API CodePack/Components/Core/AppRestartRecovery/RecoveryData.cs +++ b/Source/Current/Windows API CodePack/Components/Core/AppRestartRecovery/RecoveryData.cs @@ -48,6 +48,6 @@ public RecoveryData(RecoveryCallback callback, object state) /// public void Invoke() { - if (Callback != null) { Callback(State); } + Callback?.Invoke(State); } } \ No newline at end of file diff --git a/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialog.cs b/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialog.cs index 6a6473c6..483f1f40 100644 --- a/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialog.cs +++ b/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialog.cs @@ -1140,10 +1140,7 @@ internal void RaiseButtonClickEvent(int id) internal void RaiseHyperlinkClickEvent(string link) { EventHandler? handler = HyperlinkClick; - if (handler != null) - { - handler(this, new TaskDialogHyperlinkClickedEventArgs(link)); - } + handler?.Invoke(this, new TaskDialogHyperlinkClickedEventArgs(link)); } // Gives event subscriber a chance to prevent @@ -1193,17 +1190,17 @@ internal int RaiseClosingEvent(int id) internal void RaiseHelpInvokedEvent() { - if (HelpInvoked != null) { HelpInvoked(this, EventArgs.Empty); } + HelpInvoked?.Invoke(this, EventArgs.Empty); } internal void RaiseOpenedEvent() { - if (Opened != null) { Opened(this, EventArgs.Empty); } + Opened?.Invoke(this, EventArgs.Empty); } internal void RaiseTickEvent(int ticks) { - if (Tick != null) { Tick(this, new TaskDialogTickEventArgs(ticks)); } + Tick?.Invoke(this, new TaskDialogTickEventArgs(ticks)); } #endregion diff --git a/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialogButtonBase.cs b/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialogButtonBase.cs index 47d1498d..54ea66a7 100644 --- a/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialogButtonBase.cs +++ b/Source/Current/Windows API CodePack/Components/Core/Dialogs/TaskDialogs/TaskDialogButtonBase.cs @@ -46,8 +46,8 @@ internal void RaiseClickEvent() { // Only perform click if the button is enabled. if (!_enabled) { return; } - - if (Click != null) { Click(this, EventArgs.Empty); } + + Click?.Invoke(this, EventArgs.Empty); } private string? _text; diff --git a/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj b/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj index 65c134ac..2590c583 100644 --- a/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj +++ b/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj @@ -29,26 +29,26 @@ DynamicLibrary true - v143 + v145 Unicode DynamicLibrary false - v143 + v145 true Unicode DynamicLibrary true - v143 + v145 Unicode DynamicLibrary false - v143 + v145 true Unicode diff --git a/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj.filters b/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj.filters index ecd244cd..99795624 100644 --- a/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj.filters +++ b/Source/Current/Windows API CodePack/Components/DirectX/DirectX.vcxproj.filters @@ -1,8 +1,10 @@  - - + + Resource Files + + diff --git a/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/Sensor.cs b/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/Sensor.cs index 9694fdce..93684c58 100644 --- a/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/Sensor.cs +++ b/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/Sensor.cs @@ -333,26 +333,24 @@ public override string ToString() /// A property value. public object? GetProperty(PropertyKey propKey) { - using (PropVariant pv = new()) + using PropVariant pv = new(); + HResult hr = _nativeISensor.GetProperty(ref propKey, pv); + if (hr != HResult.Ok) { - HResult hr = _nativeISensor.GetProperty(ref propKey, pv); - if (hr != HResult.Ok) + Exception e = Marshal.GetExceptionForHR((int)hr); + if (hr == HResult.ElementNotFound) + { + throw new ArgumentOutOfRangeException(LocalizedMessages.SensorPropertyNotFound, e); + } + else { - Exception e = Marshal.GetExceptionForHR((int)hr); - if (hr == HResult.ElementNotFound) + if (e != null) { - throw new ArgumentOutOfRangeException(LocalizedMessages.SensorPropertyNotFound, e); - } - else - { - if (e != null) - { - throw e; - } + throw e; } } - return pv.Value; } + return pv.Value; } /// @@ -403,11 +401,9 @@ public override string ToString() for (uint i = 0; i < count; i++) { PropertyKey propKey = new(); - using (PropVariant propVal = new()) - { - valuesCollection.GetAt(i, ref propKey, propVal); - data.Add(propKey, propVal.Value); - } + using PropVariant propVal = new(); + valuesCollection.GetAt(i, ref propKey, propVal); + data.Add(propKey, propVal.Value); } } finally @@ -517,13 +513,11 @@ public override string ToString() for (uint i = 0; i < count; i++) { PropertyKey propKey = new(); - using (PropVariant propVal = new()) - { - valuesCollection.GetAt(i, ref propKey, propVal); + using PropVariant propVal = new(); + valuesCollection.GetAt(i, ref propKey, propVal); - int idx = propKeyToIdx[propKey]; - data[idx] = propVal.Value; - } + int idx = propKeyToIdx[propKey]; + data[idx] = propVal.Value; } } finally @@ -570,10 +564,8 @@ public override string ToString() { // new PropVariant will throw an ArgumentException if the value can // not be converted to an appropriate PropVariant. - using (PropVariant pv = PropVariant.FromObject(value)) - { - pdv.SetValue(ref propKey, pv); - } + using PropVariant pv = PropVariant.FromObject(value); + pdv.SetValue(ref propKey, pv); } catch (ArgumentException) { @@ -607,11 +599,9 @@ public override string ToString() for (uint i = 0; i < count; i++) { PropertyKey propKey = new(); - using (PropVariant propVal = new()) - { - pdv2.GetAt(i, ref propKey, propVal); - results.Add(propKey, propVal.Value); - } + using PropVariant propVal = new(); + pdv2.GetAt(i, ref propKey, propVal); + results.Add(propKey, propVal.Value); } } finally diff --git a/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/SensorData.cs b/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/SensorData.cs index dc75e51e..ebc69c12 100644 --- a/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/SensorData.cs +++ b/Source/Current/Windows API CodePack/Components/Sensors/ObjectModel/SensorData.cs @@ -24,23 +24,21 @@ public class SensorData : IDictionary> for (uint index = 0; index < items; index++) { PropertyKey key; - using (PropVariant propValue = new()) - { - keyCollection.GetAt(index, out key); - valuesCollection?.GetValue(ref key, propValue); + using PropVariant propValue = new(); + keyCollection.GetAt(index, out key); + valuesCollection?.GetValue(ref key, propValue); - if (data.ContainsKey(key.FormatId)) - { - if (propValue.Value != null) - { - data[key.FormatId].Add(propValue.Value); - } - } - else + if (data.ContainsKey(key.FormatId)) + { + if (propValue.Value != null) { - data.Add(key.FormatId, new List { propValue.Value! }); + data[key.FormatId].Add(propValue.Value); } } + else + { + data.Add(key.FormatId, new List { propValue.Value! }); + } } if (keyCollection != null) diff --git a/Source/Current/Windows API CodePack/Components/Shell/Common/EventHandlerExtensionMethods.cs b/Source/Current/Windows API CodePack/Components/Shell/Common/EventHandlerExtensionMethods.cs index 4561964b..f9b7ac4e 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Common/EventHandlerExtensionMethods.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Common/EventHandlerExtensionMethods.cs @@ -12,10 +12,7 @@ public static class EventHandlerExtensionMethods /// Event sender public static void SafeRaise(this EventHandler? eventHandler, object? sender) { - if (eventHandler != null) - { - eventHandler(sender, EventArgs.Empty); - } + eventHandler?.Invoke(sender, EventArgs.Empty); } /// @@ -27,10 +24,7 @@ public static void SafeRaise(this EventHandler? eventHandler, object? sender) /// Event args public static void SafeRaise(this EventHandler? eventHandler, object? sender, T args) where T : EventArgs { - if (eventHandler != null) - { - eventHandler(sender, args); - } + eventHandler?.Invoke(sender, args); } /// diff --git a/Source/Current/Windows API CodePack/Components/Shell/Common/SearchCondition.cs b/Source/Current/Windows API CodePack/Components/Shell/Common/SearchCondition.cs index f99b2ebb..8ded2ce6 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Common/SearchCondition.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Common/SearchCondition.cs @@ -24,19 +24,17 @@ internal SearchCondition(ICondition? nativeSearchCondition) if (ConditionType == SearchConditionType.Leaf) { - using (PropVariant propVar = new()) - { - hr = NativeSearchCondition.GetComparisonInfo(out _canonicalName, out _conditionOperation, propVar); + using PropVariant propVar = new(); + hr = NativeSearchCondition.GetComparisonInfo(out _canonicalName, out _conditionOperation, propVar); - if (!CoreErrorHelper.Succeeded(hr)) - { - throw new ShellException(hr); - } + if (!CoreErrorHelper.Succeeded(hr)) + { + throw new ShellException(hr); + } - if (propVar.Value != null) - { - PropertyValue = propVar.Value.ToString(); - } + if (propVar.Value != null) + { + PropertyValue = propVar.Value.ToString(); } } } diff --git a/Source/Current/Windows API CodePack/Components/Shell/Common/SearchConditionFactory.cs b/Source/Current/Windows API CodePack/Components/Shell/Common/SearchConditionFactory.cs index 488ef148..f96a5265 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Common/SearchConditionFactory.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Common/SearchConditionFactory.cs @@ -26,10 +26,8 @@ public static class SearchConditionFactory /// public static SearchCondition? CreateLeafCondition(string? propertyName, string? value, SearchConditionOperation operation) { - using (PropVariant propVar = new(value)) - { - return CreateLeafCondition(propertyName, propVar, null, operation); - } + using PropVariant propVar = new(value); + return CreateLeafCondition(propertyName, propVar, null, operation); } /// @@ -48,10 +46,8 @@ public static class SearchConditionFactory /// public static SearchCondition? CreateLeafCondition(string? propertyName, DateTime value, SearchConditionOperation operation) { - using (PropVariant propVar = new(value)) - { - return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.DateTime", operation); - } + using PropVariant propVar = new(value); + return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.DateTime", operation); } /// @@ -69,10 +65,8 @@ public static class SearchConditionFactory /// public static SearchCondition? CreateLeafCondition(string? propertyName, int value, SearchConditionOperation operation) { - using (PropVariant propVar = new(value)) - { - return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.Integer", operation); - } + using PropVariant propVar = new(value); + return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.Integer", operation); } /// @@ -90,10 +84,8 @@ public static class SearchConditionFactory /// public static SearchCondition? CreateLeafCondition(string? propertyName, bool value, SearchConditionOperation operation) { - using (PropVariant propVar = new(value)) - { - return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.Boolean", operation); - } + using PropVariant propVar = new(value); + return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.Boolean", operation); } /// @@ -111,10 +103,8 @@ public static class SearchConditionFactory /// public static SearchCondition? CreateLeafCondition(string? propertyName, double value, SearchConditionOperation operation) { - using (PropVariant propVar = new(value)) - { - return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.FloatingPoint", operation); - } + using PropVariant propVar = new(value); + return CreateLeafCondition(propertyName, propVar, "System.StructuredQuery.CustomProperty.FloatingPoint", operation); } [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] diff --git a/Source/Current/Windows API CodePack/Components/Shell/Common/ShellLibrary.cs b/Source/Current/Windows API CodePack/Components/Shell/Common/ShellLibrary.cs index bcd9efb4..593ffab4 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Common/ShellLibrary.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Common/ShellLibrary.cs @@ -543,10 +543,8 @@ public static void ShowManageLibraryUi(string? libraryName, string folderPath, I // Access Violations if called from an MTA thread so we wrap this // call up into a Worker thread that performs all operations in a // single threaded apartment - using (ShellLibrary shellLibrary = Load(libraryName, folderPath, true)) - { - ShowManageLibraryUi(shellLibrary, windowHandle, title, instruction, allowAllLocations); - } + using ShellLibrary shellLibrary = Load(libraryName, folderPath, true); + ShowManageLibraryUi(shellLibrary, windowHandle, title, instruction, allowAllLocations); } /// @@ -564,10 +562,8 @@ public static void ShowManageLibraryUi(string? libraryName, IntPtr windowHandle, // Access Violations if called from an MTA thread so we wrap this // call up into a Worker thread that performs all operations in a // single threaded apartment - using (ShellLibrary shellLibrary = Load(libraryName, true)) - { - ShowManageLibraryUi(shellLibrary, windowHandle, title, instruction, allowAllLocations); - } + using ShellLibrary shellLibrary = Load(libraryName, true); + ShowManageLibraryUi(shellLibrary, windowHandle, title, instruction, allowAllLocations); } /// @@ -585,10 +581,8 @@ public static void ShowManageLibraryUi(IKnownFolder? sourceKnownFolder, IntPtr w // Access Violations if called from an MTA thread so we wrap this // call up into a Worker thread that performs all operations in a // single threaded apartment - using (ShellLibrary shellLibrary = Load(sourceKnownFolder, true)) - { - ShowManageLibraryUi(shellLibrary, windowHandle, title, instruction, allowAllLocations); - } + using ShellLibrary shellLibrary = Load(sourceKnownFolder, true); + ShowManageLibraryUi(shellLibrary, windowHandle, title, instruction, allowAllLocations); } #endregion diff --git a/Source/Current/Windows API CodePack/Components/Shell/CommonFileDialogs/CommonFileDialog.cs b/Source/Current/Windows API CodePack/Components/Shell/CommonFileDialogs/CommonFileDialog.cs index 831d7e65..da947d00 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/CommonFileDialogs/CommonFileDialog.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/CommonFileDialogs/CommonFileDialog.cs @@ -1062,10 +1062,7 @@ private void GetCustomizedFileDialog() protected virtual void OnFileOk(CancelEventArgs e) { CancelEventHandler? handler = FileOk; - if (handler != null) - { - handler(this, e); - } + handler?.Invoke(this, e); } /// /// Raises the to stop navigation to a particular location. @@ -1074,10 +1071,7 @@ protected virtual void OnFileOk(CancelEventArgs e) protected virtual void OnFolderChanging(CommonFileDialogFolderChangeEventArgs e) { EventHandler? handler = FolderChanging; - if (handler != null) - { - handler(this, e); - } + handler?.Invoke(this, e); } /// /// Raises the event when the user navigates to a new folder. @@ -1086,10 +1080,7 @@ protected virtual void OnFolderChanging(CommonFileDialogFolderChangeEventArgs e) protected virtual void OnFolderChanged(EventArgs e) { EventHandler? handler = FolderChanged; - if (handler != null) - { - handler(this, e); - } + handler?.Invoke(this, e); } /// /// Raises the event when the user changes the selection in the dialog's view. @@ -1098,10 +1089,7 @@ protected virtual void OnFolderChanged(EventArgs e) protected virtual void OnSelectionChanged(/*EventArgs*/CommonFileDialogSelectionChangedEventArgs e) { var handler = SelectionChanged; - if (handler != null) - { - handler(this, e); - } + handler?.Invoke(this, e); } /// /// Raises the event when the dialog is opened to notify the @@ -1111,10 +1099,7 @@ protected virtual void OnSelectionChanged(/*EventArgs*/CommonFileDialogSelection protected virtual void OnFileTypeChanged(EventArgs e) { EventHandler? handler = FileTypeChanged; - if (handler != null) - { - handler(this, e); - } + handler?.Invoke(this, e); } /// /// Raises the event when the dialog is opened. @@ -1123,10 +1108,7 @@ protected virtual void OnFileTypeChanged(EventArgs e) protected virtual void OnOpening(EventArgs e) { EventHandler? handler = DialogOpening; - if (handler != null) - { - handler(this, e); - } + handler?.Invoke(this, e); } #endregion diff --git a/Source/Current/Windows API CodePack/Components/Shell/Controls/CommandLinkWPF.xaml.cs b/Source/Current/Windows API CodePack/Components/Shell/Controls/CommandLinkWPF.xaml.cs index dbc899ca..5a94d37d 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Controls/CommandLinkWPF.xaml.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Controls/CommandLinkWPF.xaml.cs @@ -24,10 +24,7 @@ public CommandLink() void button_Click(object? sender, RoutedEventArgs e) { e.Source = this; - if (Click != null) - { - Click(sender, e); - } + Click?.Invoke(sender, e); } /// @@ -52,10 +49,7 @@ public string? Link { _link = value; - if (PropertyChanged != null) - { - PropertyChanged(this, new PropertyChangedEventArgs("Link")); - } + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Link")); } } private string? _note; @@ -69,10 +63,7 @@ public string? Note set { _note = value; - if (PropertyChanged != null) - { - PropertyChanged(this, new PropertyChangedEventArgs("Note")); - } + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Note")); } } private ImageSource? _icon; @@ -86,10 +77,7 @@ public ImageSource? Icon set { _icon = value; - if (PropertyChanged != null) - { - PropertyChanged(this, new PropertyChangedEventArgs("Icon")); - } + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Icon")); } } diff --git a/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowser.cs b/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowser.cs index e336c2fb..af3f0efe 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowser.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowser.cs @@ -1574,10 +1574,7 @@ internal void FireContentChanged() internal void FireSelectionChanged() { - if (SelectionChanged != null) - { - SelectionChanged(this, EventArgs.Empty); - } + SelectionChanged?.Invoke(this, EventArgs.Empty); } internal void FireSelectedItemChanged() diff --git a/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowserNavigationLog.cs b/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowserNavigationLog.cs index 3a8b45c6..a220d3a4 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowserNavigationLog.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/ExplorerBrowser/ExplorerBrowserNavigationLog.cs @@ -29,10 +29,7 @@ public void ClearLog() CanNavigateBackwardChanged = (oldCanNavigateBackward != CanNavigateBackward), CanNavigateForwardChanged = (oldCanNavigateForward != CanNavigateForward) }; - if (NavigationLogChanged != null) - { - NavigationLogChanged(this, args); - } + NavigationLogChanged?.Invoke(this, args); } #endregion @@ -167,10 +164,7 @@ private void OnNavigationComplete(object? sender, NavigationCompleteEventArgs ar eventArgs.CanNavigateBackwardChanged = (oldCanNavigateBackward != CanNavigateBackward); eventArgs.CanNavigateForwardChanged = (oldCanNavigateForward != CanNavigateForward); - if (NavigationLogChanged != null) - { - NavigationLogChanged(this, eventArgs); - } + NavigationLogChanged?.Invoke(this, eventArgs); } internal bool NavigateLog(NavigationLogDirection direction) diff --git a/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellProperty.cs b/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellProperty.cs index ba2591b6..86e4d584 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellProperty.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellProperty.cs @@ -36,28 +36,26 @@ private void GetImageReference() { IPropertyStore? store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject); - using (PropVariant propVar = new()) - { - store.GetValue(ref _propertyKey, propVar); + using PropVariant propVar = new(); + store.GetValue(ref _propertyKey, propVar); - if (store != null) - { - Marshal.ReleaseComObject(store); - } - store = null; + if (store != null) + { + Marshal.ReleaseComObject(store); + } + store = null; - string? refPath; - ((IPropertyDescription2)Description?.NativePropertyDescription!).GetImageReferenceForValue( - propVar, out refPath); + string? refPath; + ((IPropertyDescription2)Description?.NativePropertyDescription!).GetImageReferenceForValue( + propVar, out refPath); - if (refPath == null) { return; } + if (refPath == null) { return; } - int index = ShellNativeMethods.PathParseIconLocation(ref refPath); - if (refPath != null) - { - _imageReferencePath = refPath; - _imageReferenceIconIndex = index; - } + int index = ShellNativeMethods.PathParseIconLocation(ref refPath); + if (refPath != null) + { + _imageReferencePath = refPath; + _imageReferenceIconIndex = index; } } @@ -171,27 +169,25 @@ public T? Value // Make sure we load the correct type Debug.Assert(ValueType == ShellPropertyFactory.VarEnumToSystemType(Description!.VarEnumType)); - using (PropVariant propVar = new()) + using PropVariant propVar = new(); + if (ParentShellObject?.NativePropertyStore! != null) { - if (ParentShellObject?.NativePropertyStore! != null) - { - // If there is a valid property store for this shell object, then use it. - ParentShellObject.NativePropertyStore.GetValue(ref _propertyKey, propVar); - } - else if (ParentShellObject != null) - { - // Use IShellItem2.GetProperty instead of creating a new property store - // The file might be locked. This is probably quicker, and sufficient for what we need - ParentShellObject?.NativeShellItem2!.GetProperty(ref _propertyKey, propVar); - } - else if (NativePropertyStore != null) - { - NativePropertyStore?.GetValue(ref _propertyKey, propVar); - } - - //Get the value - return propVar.Value != null ? (T)propVar.Value : default(T); + // If there is a valid property store for this shell object, then use it. + ParentShellObject.NativePropertyStore.GetValue(ref _propertyKey, propVar); } + else if (ParentShellObject != null) + { + // Use IShellItem2.GetProperty instead of creating a new property store + // The file might be locked. This is probably quicker, and sufficient for what we need + ParentShellObject?.NativeShellItem2!.GetProperty(ref _propertyKey, propVar); + } + else if (NativePropertyStore != null) + { + NativePropertyStore?.GetValue(ref _propertyKey, propVar); + } + + //Get the value + return propVar.Value != null ? (T)propVar.Value : default(T); } set { @@ -227,10 +223,8 @@ public T? Value if (ParentShellObject != null) { - using (ShellPropertyWriter propertyWriter = ParentShellObject?.Properties!.GetPropertyWriter()!) - { - propertyWriter.WriteProperty(this, value, AllowSetTruncatedValue); - } + using ShellPropertyWriter propertyWriter = ParentShellObject?.Properties!.GetPropertyWriter()!; + propertyWriter.WriteProperty(this, value, AllowSetTruncatedValue); } else if (NativePropertyStore != null) { @@ -311,27 +305,25 @@ public bool TryFormatForDisplay(PropertyDescriptionFormatOptions format, out str IPropertyStore? store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject); - using (PropVariant propVar = new()) - { - store.GetValue(ref _propertyKey, propVar); - - // Release the Propertystore - if (store != null) - { - Marshal.ReleaseComObject(store); - } + using PropVariant propVar = new(); + store.GetValue(ref _propertyKey, propVar); - HResult hr = Description.NativePropertyDescription.FormatForDisplay(propVar, ref format, out formattedString); + // Release the Propertystore + if (store != null) + { + Marshal.ReleaseComObject(store); + } - // Sometimes, the value cannot be displayed properly, such as for blobs - // or if we get argument exception - if (!CoreErrorHelper.Succeeded(hr)) - { - throw new ShellException(hr); - } + HResult hr = Description.NativePropertyDescription.FormatForDisplay(propVar, ref format, out formattedString); - return formattedString; + // Sometimes, the value cannot be displayed properly, such as for blobs + // or if we get argument exception + if (!CoreErrorHelper.Succeeded(hr)) + { + throw new ShellException(hr); } + + return formattedString; } /// @@ -350,10 +342,8 @@ public bool TryFormatForDisplay(PropertyDescriptionFormatOptions format, out str /// public void ClearValue() { - using (PropVariant propVar = new()) - { - StorePropVariantValue(propVar); - } + using PropVariant propVar = new(); + StorePropVariantValue(propVar); } /// @@ -366,27 +356,25 @@ public object? ValueAsObject { get { - using (PropVariant propVar = new()) + using PropVariant propVar = new(); + if (ParentShellObject != null) { - if (ParentShellObject != null) - { - IPropertyStore? store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject); + IPropertyStore? store = ShellPropertyCollection.CreateDefaultPropertyStore(ParentShellObject); - store.GetValue(ref _propertyKey, propVar); + store.GetValue(ref _propertyKey, propVar); - if (store != null) - { - Marshal.ReleaseComObject(store); - } - } - else + if (store != null) { - NativePropertyStore?.GetValue(ref _propertyKey, propVar); + Marshal.ReleaseComObject(store); } - - return propVar?.Value; } + else + { + NativePropertyStore?.GetValue(ref _propertyKey, propVar); + } + + return propVar?.Value; } } diff --git a/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyEnumType.cs b/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyEnumType.cs index 2bb3940c..a56d8032 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyEnumType.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyEnumType.cs @@ -75,11 +75,9 @@ public object? RangeMinValue { if (_minValue == null) { - using (PropVariant propVar = new()) - { - NativePropertyEnumType.GetRangeMinValue(propVar); - _minValue = propVar.Value; - } + using PropVariant propVar = new(); + NativePropertyEnumType.GetRangeMinValue(propVar); + _minValue = propVar.Value; } return _minValue; @@ -95,11 +93,9 @@ public object? RangeSetValue { if (_setValue == null) { - using (PropVariant propVar = new()) - { - NativePropertyEnumType.GetRangeSetValue(propVar); - _setValue = propVar.Value; - } + using PropVariant propVar = new(); + NativePropertyEnumType.GetRangeSetValue(propVar); + _setValue = propVar.Value; } return _setValue; @@ -115,11 +111,9 @@ public object? RangeValue { if (_enumerationValue == null) { - using (PropVariant propVar = new()) - { - NativePropertyEnumType.GetValue(propVar); - _enumerationValue = propVar.Value; - } + using PropVariant propVar = new(); + NativePropertyEnumType.GetValue(propVar); + _enumerationValue = propVar.Value; } return _enumerationValue; } diff --git a/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyWriter.cs b/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyWriter.cs index 12b2a303..a3e064e4 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyWriter.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/PropertySystem/ShellPropertyWriter.cs @@ -95,25 +95,23 @@ public void WriteProperty(PropertyKey key, object? value, bool allowTruncatedVal throw new InvalidOperationException("Writeable store has been closed."); } - using (PropVariant propVar = PropVariant.FromObject(value)) - { - HResult result = WritablePropStore.SetValue(ref key, propVar); + using PropVariant propVar = PropVariant.FromObject(value); + HResult result = WritablePropStore.SetValue(ref key, propVar); - if (!allowTruncatedValue && ((int)result == ShellNativeMethods.InPlaceStringTruncated)) - { - // At this point we can't revert back the commit - // so don't commit, close the property store and throw an exception - // to let the user know. - Marshal.ReleaseComObject(WritablePropStore); - WritablePropStore = null; + if (!allowTruncatedValue && ((int)result == ShellNativeMethods.InPlaceStringTruncated)) + { + // At this point we can't revert back the commit + // so don't commit, close the property store and throw an exception + // to let the user know. + Marshal.ReleaseComObject(WritablePropStore); + WritablePropStore = null; - throw new ArgumentOutOfRangeException(nameof(value), LocalizedMessages.ShellPropertyValueTruncated); - } + throw new ArgumentOutOfRangeException(nameof(value), LocalizedMessages.ShellPropertyValueTruncated); + } - if (!CoreErrorHelper.Succeeded(result)) - { - throw new PropertySystemException(LocalizedMessages.ShellPropertySetValue, Marshal.GetExceptionForHR((int)result)); - } + if (!CoreErrorHelper.Succeeded(result)) + { + throw new PropertySystemException(LocalizedMessages.ShellPropertySetValue, Marshal.GetExceptionForHR((int)result)); } } diff --git a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnail.cs b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnail.cs index 471cf923..83b651be 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnail.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnail.cs @@ -5,6 +5,7 @@ namespace Microsoft.WindowsAPICodePack.Taskbar; /// /// Represents a tabbed thumbnail on the taskbar for a given window or a control. /// +/// public class TabbedThumbnail : IDisposable { #region Internal members @@ -147,7 +148,7 @@ public string? Title if (_title != value) { _title = value; - if (TitleChanged != null) { TitleChanged(this, EventArgs.Empty); } + TitleChanged?.Invoke(this, EventArgs.Empty); } } } @@ -155,7 +156,7 @@ public string? Title private string? _tooltip = string.Empty; /// /// Tooltip to be shown for this thumbnail on the taskbar. - /// By default this is full title of the window shown on the taskbar. + /// By default, this is full title of the window shown on the taskbar. /// public string? Tooltip { @@ -165,7 +166,7 @@ public string? Tooltip if (_tooltip != value) { _tooltip = value; - if (TooltipChanged != null) { TooltipChanged(this, EventArgs.Empty); } + TooltipChanged?.Invoke(this, EventArgs.Empty); } } } @@ -359,6 +360,8 @@ public void InvalidatePreview() /// public event EventHandler? TabbedThumbnailClosed; + public event EventHandler? TabbedThumbnailClosing; + /// /// The event that occurs when a tab is maximized via the taskbar thumbnail preview (context menu). /// @@ -415,7 +418,7 @@ internal void OnTabbedThumbnailMinimized() /// Returns true if the thumbnail was removed from the taskbar; false if it was not. internal bool OnTabbedThumbnailClosed() { - var closedHandler = TabbedThumbnailClosed; + var closedHandler = TabbedThumbnailClosing; if (closedHandler != null) { var closingEvent = GetTabbedThumbnailClosingEventArgs(); @@ -468,17 +471,17 @@ internal void OnTabbedThumbnailBitmapRequested() } } - private TabbedThumbnailClosedEventArgs? GetTabbedThumbnailClosingEventArgs() + private TabbedThumbnailClosingEventArgs? GetTabbedThumbnailClosingEventArgs() { - TabbedThumbnailClosedEventArgs? eventArgs = null; + TabbedThumbnailClosingEventArgs? eventArgs = null; if (WindowHandle != IntPtr.Zero) { - eventArgs = new TabbedThumbnailClosedEventArgs(WindowHandle); + eventArgs = new TabbedThumbnailClosingEventArgs(WindowHandle); } else if (WindowsControl != null) { - eventArgs = new TabbedThumbnailClosedEventArgs(WindowsControl); + eventArgs = new TabbedThumbnailClosingEventArgs(WindowsControl); } return eventArgs; diff --git a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosedEventArgs.cs b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosedEventArgs.cs index a6909b17..68e25098 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosedEventArgs.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosedEventArgs.cs @@ -17,9 +17,4 @@ public TabbedThumbnailClosedEventArgs(IntPtr windowHandle) : base(windowHandle) /// WPF Control (UIElement) related to the event public TabbedThumbnailClosedEventArgs(UIElement? windowsControl) : base(windowsControl) { } - /// - /// If set to true, the proxy window will not be removed from the taskbar. - /// - public bool Cancel { get; set; } - } \ No newline at end of file diff --git a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosingEventArgs.cs b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosingEventArgs.cs index 908ba0c7..caabc8aa 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosingEventArgs.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailClosingEventArgs.cs @@ -1,7 +1,7 @@  -namespace Microsoft.WindowsAPICodePack.Shell.Taskbar; +namespace Microsoft.WindowsAPICodePack.Taskbar; -/// +// /// Event args for the TabbedThumbnailClosing event. The application should set /// the Cancel property to true if the thumbnail should not be removed. /// @@ -33,4 +33,4 @@ public bool Cancel get; set; } -} \ No newline at end of file +} diff --git a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailManager.cs b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailManager.cs index f127cc9b..a93d0c15 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailManager.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailManager.cs @@ -151,9 +151,9 @@ public void RemoveThumbnailPreview(IntPtr windowHandle) if (taskBarWindow != null) { - if (TaskbarWindowManager._taskbarWindowList.Contains(taskBarWindow)) + if (TaskbarWindowManager.TaskbarWindowList.Contains(taskBarWindow)) { - TaskbarWindowManager._taskbarWindowList.Remove(taskBarWindow); + TaskbarWindowManager.TaskbarWindowList.Remove(taskBarWindow); } taskBarWindow.Dispose(); } @@ -198,9 +198,9 @@ public void RemoveThumbnailPreview(UIElement? windowsControl) if (taskbarWindow != null) { - if (TaskbarWindowManager._taskbarWindowList.Contains(taskbarWindow)) + if (TaskbarWindowManager.TaskbarWindowList.Contains(taskbarWindow)) { - TaskbarWindowManager._taskbarWindowList.Remove(taskbarWindow); + TaskbarWindowManager.TaskbarWindowList.Remove(taskbarWindow); } taskbarWindow.Dispose(); } diff --git a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailScreenCapture.cs b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailScreenCapture.cs index 4e218b11..c5beb85c 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailScreenCapture.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TabbedThumbnailScreenCapture.cs @@ -49,24 +49,22 @@ public static class TabbedThumbnailScreenCapture targetBitmap = new Bitmap(size.Width, size.Height); - using (Graphics targetGr = Graphics.FromImage(targetBitmap)) - { - IntPtr targetDC = targetGr.GetHdc(); - uint operation = 0x00CC0020 /*SRCCOPY*/; + using Graphics targetGr = Graphics.FromImage(targetBitmap); + IntPtr targetDC = targetGr.GetHdc(); + uint operation = 0x00CC0020 /*SRCCOPY*/; - Size ncArea = WindowUtilities.GetNonClientArea(windowHandle); + Size ncArea = WindowUtilities.GetNonClientArea(windowHandle); - bool success = TabbedThumbnailNativeMethods.StretchBlt( - targetDC, 0, 0, targetBitmap.Width, targetBitmap.Height, - windowDC, ncArea.Width, ncArea.Height, realWindowSize.Width, - realWindowSize.Height, operation); + bool success = TabbedThumbnailNativeMethods.StretchBlt( + targetDC, 0, 0, targetBitmap.Width, targetBitmap.Height, + windowDC, ncArea.Width, ncArea.Height, realWindowSize.Width, + realWindowSize.Height, operation); - targetGr.ReleaseHdc(targetDC); + targetGr.ReleaseHdc(targetDC); - if (!success) { return null; } + if (!success) { return null; } - return targetBitmap; - } + return targetBitmap; } catch { diff --git a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TaskbarWindowManager.cs b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TaskbarWindowManager.cs index f495f02e..2cd8c4b4 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TaskbarWindowManager.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/TaskbarWindowManager.cs @@ -10,7 +10,7 @@ namespace Microsoft.WindowsAPICodePack.Taskbar; internal static class TaskbarWindowManager { - internal static List _taskbarWindowList = new(); + internal static IList TaskbarWindowList = new List(); private static bool _buttonsAdded; @@ -56,7 +56,7 @@ private static void AddThumbnailButtons(TaskbarWindow? taskbarWindow, bool add, { if (add) { - _taskbarWindowList.Add(taskbarWindow); + TaskbarWindowList.Add(taskbarWindow); } else if (taskbarWindow!.ThumbnailButtons == null) { @@ -81,7 +81,7 @@ internal static void AddTabbedThumbnail(TabbedThumbnail? preview) if (taskbarWindow == null) { taskbarWindow = new TaskbarWindow(preview); - _taskbarWindowList.Add(taskbarWindow); + TaskbarWindowList.Add(taskbarWindow); } else if (taskbarWindow.TabbedThumbnail == null) { @@ -126,7 +126,7 @@ internal static void AddTabbedThumbnail(TabbedThumbnail? preview) { if (windowsControl == null) { throw new ArgumentNullException(nameof(windowsControl)); } - TaskbarWindow? toReturn = _taskbarWindowList.FirstOrDefault(window => (window?.TabbedThumbnail != null && window.TabbedThumbnail.WindowsControl == windowsControl) || + TaskbarWindow? toReturn = TaskbarWindowList.FirstOrDefault(window => (window?.TabbedThumbnail != null && window.TabbedThumbnail.WindowsControl == windowsControl) || (window?.ThumbnailToolbarProxyWindow != null && window.ThumbnailToolbarProxyWindow.WindowsControl == windowsControl)); @@ -153,7 +153,7 @@ internal static void AddTabbedThumbnail(TabbedThumbnail? preview) throw new ArgumentException(LocalizedMessages.CommonFileDialogInvalidHandle, nameof(userWindowHandle)); } - TaskbarWindow? toReturn = _taskbarWindowList.FirstOrDefault(window => window?.UserWindowHandle == userWindowHandle); + TaskbarWindow? toReturn = TaskbarWindowList.FirstOrDefault(window => window?.UserWindowHandle == userWindowHandle); // If it's not in the list, return null so it can be added. if (toReturn != null) @@ -504,9 +504,9 @@ private static bool DispatchNCDestroyMessage(ref Message m, TaskbarWindow? taskb taskbarWindow!.TabbedThumbnail?.OnTabbedThumbnailClosed(); // Remove the taskbar window from our internal list - if (_taskbarWindowList.Contains(taskbarWindow)) + if (TaskbarWindowList.Contains(taskbarWindow)) { - _taskbarWindowList.Remove(taskbarWindow); + TaskbarWindowList.Remove(taskbarWindow); } taskbarWindow.Dispose(); @@ -526,9 +526,9 @@ private static bool DispatchSystemCommandMessage(ref Message m, TaskbarWindow? t if (taskbarWindow!.TabbedThumbnail!.OnTabbedThumbnailClosed()) { // Remove the taskbar window from our internal list - if (_taskbarWindowList.Contains(taskbarWindow)) + if (TaskbarWindowList.Contains(taskbarWindow)) { - _taskbarWindowList.Remove(taskbarWindow); + TaskbarWindowList.Remove(taskbarWindow); } taskbarWindow.Dispose(); diff --git a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/ThumbnailButton.cs b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/ThumbnailButton.cs index d1c3471c..f0f3d91c 100644 --- a/Source/Current/Windows API CodePack/Components/Shell/Taskbar/ThumbnailButton.cs +++ b/Source/Current/Windows API CodePack/Components/Shell/Taskbar/ThumbnailButton.cs @@ -234,7 +234,7 @@ internal ThumbButton Win32ThumbButton { _win32ThumbButton.Id = Id; _win32ThumbButton.Tip = Tooltip; - _win32ThumbButton.Icon = Icon != null ? Icon.Handle : IntPtr.Zero; + _win32ThumbButton.Icon = Icon?.Handle ?? IntPtr.Zero; _win32ThumbButton.Flags = Flags; _win32ThumbButton.Mask = ThumbButtonMask.ThbFlags; diff --git a/Source/Current/Windows API CodePack/Components/ShellExtensions/PreviewHandlers/PreviewHandler.cs b/Source/Current/Windows API CodePack/Components/ShellExtensions/PreviewHandlers/PreviewHandler.cs index 43264901..e20eca44 100644 --- a/Source/Current/Windows API CodePack/Components/ShellExtensions/PreviewHandlers/PreviewHandler.cs +++ b/Source/Current/Windows API CodePack/Components/ShellExtensions/PreviewHandlers/PreviewHandler.cs @@ -185,10 +185,9 @@ void IInitializeWithStream.Initialize(IStream? stream, AccessModes fileMode) LocalizedMessages.PreviewHandlerUnsupportedInterfaceCalled, "IPreviewFromStream")); } - using (var storageStream = new StorageStream(stream, fileMode != AccessModes.ReadWrite)) - { - preview.Load(storageStream); - } + + using var storageStream = new StorageStream(stream, fileMode != AccessModes.ReadWrite); + preview.Load(storageStream); } #endregion @@ -204,10 +203,9 @@ void IInitializeWithItem.Initialize(IShellItem? shellItem, AccessModes accessMod LocalizedMessages.PreviewHandlerUnsupportedInterfaceCalled, "IPreviewFromShellObject")); } - using (var shellObject = ShellObjectFactory.Create(shellItem)) - { - preview.Load(shellObject); - } + + using var shellObject = ShellObjectFactory.Create(shellItem); + preview.Load(shellObject); } #endregion @@ -307,12 +305,10 @@ private static void RegisterPreviewHandler(Guid previewerGuid, PreviewHandlerAtt Trace.WriteLine("Registering extension '" + extension + "' with previewer '" + guid + "'"); // Set preview handler for specific extension - using (RegistryKey extensionKey = Registry.ClassesRoot.CreateSubKey(extension)) - using (RegistryKey shellexKey = extensionKey.CreateSubKey("shellex")) - using (RegistryKey previewKey = shellexKey.CreateSubKey(HandlerNativeMethods.IPreviewHandlerGuid.ToString("B"))) - { - previewKey.SetValue(null, guid, RegistryValueKind.String); - } + using RegistryKey extensionKey = Registry.ClassesRoot.CreateSubKey(extension); + using RegistryKey shellexKey = extensionKey.CreateSubKey("shellex"); + using RegistryKey previewKey = shellexKey.CreateSubKey(HandlerNativeMethods.IPreviewHandlerGuid.ToString("B")); + previewKey.SetValue(null, guid, RegistryValueKind.String); } } @@ -322,10 +318,8 @@ private static void UnregisterPreviewHandler(Guid previewerGuid, PreviewHandlerA foreach (string extension in attribute.Extensions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { Trace.WriteLine("Unregistering extension '" + extension + "' with previewer '" + guid + "'"); - using (RegistryKey shellexKey = Registry.ClassesRoot.OpenSubKey(extension + "\\shellex", true)) - { - shellexKey.DeleteSubKey(HandlerNativeMethods.IPreviewHandlerGuid.ToString(), false); - } + using RegistryKey shellexKey = Registry.ClassesRoot.OpenSubKey(extension + "\\shellex", true); + shellexKey.DeleteSubKey(HandlerNativeMethods.IPreviewHandlerGuid.ToString(), false); } using (RegistryKey appIdsKey = Registry.ClassesRoot.OpenSubKey("AppID", true)) diff --git a/Source/Current/Windows API CodePack/Components/ShellExtensions/ThumbnailProviders/ThumbnailProvider.cs b/Source/Current/Windows API CodePack/Components/ShellExtensions/ThumbnailProviders/ThumbnailProvider.cs index 310c0798..7c6fc0fa 100644 --- a/Source/Current/Windows API CodePack/Components/ShellExtensions/ThumbnailProviders/ThumbnailProvider.cs +++ b/Source/Current/Windows API CodePack/Components/ShellExtensions/ThumbnailProviders/ThumbnailProvider.cs @@ -130,10 +130,8 @@ private static void RegisterThumbnailHandler(string guid, ThumbnailProviderAttri if (guidKey != null) { - using (RegistryKey inproc = guidKey.OpenSubKey("InprocServer32", true)) - { - inproc.SetValue("ThreadingModel", "Apartment", RegistryValueKind.String); - } + using RegistryKey inproc = guidKey.OpenSubKey("InprocServer32", true); + inproc.SetValue("ThreadingModel", "Apartment", RegistryValueKind.String); } } @@ -148,35 +146,33 @@ private static void RegisterThumbnailHandler(string guid, ThumbnailProviderAttri string[] extensions = attribute.Extensions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string extension in extensions) { - using (RegistryKey extensionKey = Registry.ClassesRoot.CreateSubKey(extension)) // Create makes it writable - using (RegistryKey shellExKey = extensionKey.CreateSubKey("shellex")) - using (RegistryKey providerKey = shellExKey.CreateSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B"))) + using RegistryKey extensionKey = Registry.ClassesRoot.CreateSubKey(extension); + using RegistryKey shellExKey = extensionKey.CreateSubKey("shellex"); + using RegistryKey providerKey = shellExKey.CreateSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B")); + providerKey.SetValue(null, guid, RegistryValueKind.String); + + if (attribute.ThumbnailCutoff == ThumbnailCutoffSize.Square20) + { + extensionKey.DeleteValue("ThumbnailCutoff", false); + } + else + { + extensionKey.SetValue("ThumbnailCutoff", (int)attribute.ThumbnailCutoff, RegistryValueKind.DWord); + } + + + if (attribute.TypeOverlay != null) + { + extensionKey.SetValue("TypeOverlay", attribute.TypeOverlay, RegistryValueKind.String); + } + + if (attribute.ThumbnailAdornment == ThumbnailAdornment.Default) + { + extensionKey.DeleteValue("Treatment", false); + } + else { - providerKey.SetValue(null, guid, RegistryValueKind.String); - - if (attribute.ThumbnailCutoff == ThumbnailCutoffSize.Square20) - { - extensionKey.DeleteValue("ThumbnailCutoff", false); - } - else - { - extensionKey.SetValue("ThumbnailCutoff", (int)attribute.ThumbnailCutoff, RegistryValueKind.DWord); - } - - - if (attribute.TypeOverlay != null) - { - extensionKey.SetValue("TypeOverlay", attribute.TypeOverlay, RegistryValueKind.String); - } - - if (attribute.ThumbnailAdornment == ThumbnailAdornment.Default) - { - extensionKey.DeleteValue("Treatment", false); - } - else - { - extensionKey.SetValue("Treatment", (int)attribute.ThumbnailAdornment, RegistryValueKind.DWord); - } + extensionKey.SetValue("Treatment", (int)attribute.ThumbnailAdornment, RegistryValueKind.DWord); } } } @@ -205,22 +201,18 @@ private static void UnregisterThumbnailHandler(string guid, ThumbnailProviderAtt string[] extensions = attribute.Extensions.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string extension in extensions) { - using (RegistryKey extKey = Registry.ClassesRoot.OpenSubKey(extension, true)) - using (RegistryKey shellexKey = extKey.OpenSubKey("shellex", true)) - { - shellexKey.DeleteSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B"), false); + using RegistryKey extKey = Registry.ClassesRoot.OpenSubKey(extension, true); + using RegistryKey shellexKey = extKey.OpenSubKey("shellex", true); + shellexKey.DeleteSubKey(HandlerNativeMethods.IThumbnailProviderGuid.ToString("B"), false); - extKey.DeleteValue("ThumbnailCutoff", false); - extKey.DeleteValue("TypeOverlay", false); - extKey.DeleteValue("Treatment", false); // Thumbnail adornment - } + extKey.DeleteValue("ThumbnailCutoff", false); + extKey.DeleteValue("TypeOverlay", false); + extKey.DeleteValue("Treatment", false); // Thumbnail adornment } - using (RegistryKey approvedShellExtensions = Registry.LocalMachine.OpenSubKey( - @"SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true)) - { - approvedShellExtensions.DeleteValue(guid, false); - } + using RegistryKey approvedShellExtensions = Registry.LocalMachine.OpenSubKey( + @"SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved", true); + approvedShellExtensions.DeleteValue(guid, false); } private static void ThrowIfInvalid(Type type, ThumbnailProviderAttribute? attribute) diff --git a/Source/Current/Windows API CodePack/Directory.Build.targets b/Source/Current/Windows API CodePack/Directory.Build.targets index 0536f506..996327fe 100644 --- a/Source/Current/Windows API CodePack/Directory.Build.targets +++ b/Source/Current/Windows API CodePack/Directory.Build.targets @@ -1,10 +1,10 @@ - 8.0.17.11 + 8.0.15.2 - 8.0.17 + 8.0.15.2 - 8.0.17.11 + 8.0.15.2 preview diff --git a/Source/Current/Windows API CodePack/ScratchProject/Form1.cs b/Source/Current/Windows API CodePack/ScratchProject/Form1.cs index 16b93c97..38976e4f 100644 --- a/Source/Current/Windows API CodePack/ScratchProject/Form1.cs +++ b/Source/Current/Windows API CodePack/ScratchProject/Form1.cs @@ -22,19 +22,17 @@ private void button1_Click(object? sender, EventArgs e) private void button2_Click(object? sender, EventArgs e) { - using (var dlg = new CommonSaveFileDialog()) - { - dlg.Filters.Add(new CommonFileDialogFilter(@"Word Document", "docx")); - dlg.Filters.Add(new CommonFileDialogFilter(@"Adobe PDF", "pdf")); - dlg.DefaultExtension = "docx"; + using var dlg = new CommonSaveFileDialog(); + dlg.Filters.Add(new CommonFileDialogFilter(@"Word Document", "docx")); + dlg.Filters.Add(new CommonFileDialogFilter(@"Adobe PDF", "pdf")); + dlg.DefaultExtension = "docx"; - // when dialog opens, user enters "test.docx" and then changes file type to "pdf" - // dialog correctly updates filename to "test.pdf" + // when dialog opens, user enters "test.docx" and then changes file type to "pdf" + // dialog correctly updates filename to "test.pdf" - if (dlg.ShowDialog() == CommonFileDialogResult.Ok) - { - string? wrongFileName = dlg.FileName; // value is "test.docx", WRONG - } + if (dlg.ShowDialog() == CommonFileDialogResult.Ok) + { + string? wrongFileName = dlg.FileName; // value is "test.docx", WRONG } }