Added improved registry key closing to registration classes

pull/161/head
dahall 2020-07-26 13:12:33 -06:00
parent 7f8e557241
commit ef6a1dda0a
3 changed files with 21 additions and 13 deletions

View File

@ -11,7 +11,8 @@ namespace Vanara.Windows.Shell.Registration
internal FileTypeAssociation(string ext, RegistryKey key, bool readOnly) : base(key, readOnly) internal FileTypeAssociation(string ext, RegistryKey key, bool readOnly) : base(key, readOnly)
{ {
Extension = ext; Extension = ext;
OpenWithProgIds = new RegBasedKeyCollection(key.OpenSubKey("OpenWithProgIds", !readOnly), readOnly); var owpi = readOnly ? key.OpenSubKey("OpenWithProgIds", false) : key.CreateSubKey("OpenWithProgIds", RegistryKeyPermissionCheck.ReadWriteSubTree);
OpenWithProgIds = new RegBasedKeyCollection(owpi, readOnly);
} }
/// <summary>Gets or sets the Content Type value to the file type's MIME content type.</summary> /// <summary>Gets or sets the Content Type value to the file type's MIME content type.</summary>
@ -120,8 +121,8 @@ namespace Vanara.Windows.Shell.Registration
{ {
if (extension is null) throw new ArgumentNullException(nameof(extension)); if (extension is null) throw new ArgumentNullException(nameof(extension));
if (!extension.StartsWith(".")) throw new ArgumentException("The value must be in the format \".ext\"", nameof(extension)); if (!extension.StartsWith(".")) throw new ArgumentException("The value must be in the format \".ext\"", nameof(extension));
using var sk = ShellRegistrar.GetRoot(systemWide, true); using (var sk = ShellRegistrar.GetRoot(systemWide, true))
try { sk.DeleteSubKeyTree(extension); } catch { sk.DeleteSubKey(extension, false); } try { sk.DeleteSubKeyTree(extension); } catch { sk.DeleteSubKey(extension, false); }
ShellRegistrar.NotifyShell(); ShellRegistrar.NotifyShell();
} }

View File

@ -292,8 +292,10 @@ namespace Vanara.Windows.Shell
using var root = ShellRegistrar.GetRoot(systemWide, false); using var root = ShellRegistrar.GetRoot(systemWide, false);
foreach (var ext in root.GetSubKeyNames().Where(ext => ext.StartsWith("."))) foreach (var ext in root.GetSubKeyNames().Where(ext => ext.StartsWith(".")))
{ {
using var openWithKey = root.OpenSubKey(Path.Combine(ext, OpenWithProgIds)); var hasValue = false;
if (openWithKey?.HasValue(progId) == true) using (var openWithKey = root.OpenSubKey(Path.Combine(ext, OpenWithProgIds)))
hasValue = openWithKey?.HasValue(progId) ?? false;
if (hasValue)
yield return ext; yield return ext;
} }
} }
@ -332,7 +334,8 @@ namespace Vanara.Windows.Shell
public void Add(string ext) public void Add(string ext)
{ {
EnsureWritable(); EnsureWritable();
FileTypeAssociation.Register(ext, IsSystemWide).OpenWithProgIds.Add(progId); using var assoc = FileTypeAssociation.Register(ext, IsSystemWide);
assoc.OpenWithProgIds.Add(progId);
} }
/// <summary>Clears this instance.</summary> /// <summary>Clears this instance.</summary>

View File

@ -8,7 +8,7 @@ namespace Vanara.Windows.Shell.Registration
{ {
/// <summary>A collection of values under a key that is treated as a collection of strings.</summary> /// <summary>A collection of values under a key that is treated as a collection of strings.</summary>
/// <seealso cref="System.Collections.Generic.ICollection{T}"/> /// <seealso cref="System.Collections.Generic.ICollection{T}"/>
internal class RegBasedKeyCollection : ICollection<string> internal class RegBasedKeyCollection : ICollection<string>, IDisposable
{ {
/// <summary>The base key from which to perform all queries.</summary> /// <summary>The base key from which to perform all queries.</summary>
protected internal RegistryKey key; protected internal RegistryKey key;
@ -18,13 +18,14 @@ namespace Vanara.Windows.Shell.Registration
/// <param name="readOnly">if set to <c>true</c> the supplied <paramref name="key"/> was opened read-only.</param> /// <param name="readOnly">if set to <c>true</c> the supplied <paramref name="key"/> was opened read-only.</param>
protected internal RegBasedKeyCollection(RegistryKey key, bool readOnly) protected internal RegBasedKeyCollection(RegistryKey key, bool readOnly)
{ {
this.key = key ?? throw new ArgumentNullException(nameof(key)); if (key is null && !readOnly) throw new ArgumentNullException(nameof(key));
this.key = key;
IsReadOnly = readOnly; IsReadOnly = readOnly;
} }
/// <summary>Gets the count.</summary> /// <summary>Gets the count.</summary>
/// <value>The count.</value> /// <value>The count.</value>
public int Count => key.ValueCount; public int Count => key?.ValueCount ?? 0;
/// <summary>Gets or sets a value indicating whether these settings are read-only.</summary> /// <summary>Gets or sets a value indicating whether these settings are read-only.</summary>
public bool IsReadOnly { get; } public bool IsReadOnly { get; }
@ -34,21 +35,24 @@ namespace Vanara.Windows.Shell.Registration
public void Add(string item) { EnsureWritable(); key.SetValue(item, string.Empty, RegistryValueKind.String); } public void Add(string item) { EnsureWritable(); key.SetValue(item, string.Empty, RegistryValueKind.String); }
/// <summary>Clears this instance.</summary> /// <summary>Clears this instance.</summary>
public void Clear() => key.DeleteAllSubItems(); public void Clear() { EnsureWritable(); key.DeleteAllSubItems(); }
/// <summary>Determines whether this instance contains the object.</summary> /// <summary>Determines whether this instance contains the object.</summary>
/// <param name="item">The item.</param> /// <param name="item">The item.</param>
/// <returns><see langword="true"/> if [contains] [the specified item]; otherwise, <see langword="false"/>.</returns> /// <returns><see langword="true"/> if [contains] [the specified item]; otherwise, <see langword="false"/>.</returns>
public bool Contains(string item) => key.HasValue(item); public bool Contains(string item) => key?.HasValue(item) ?? false;
/// <summary>Copies to.</summary> /// <summary>Copies to.</summary>
/// <param name="array">The array.</param> /// <param name="array">The array.</param>
/// <param name="arrayIndex">Index of the array.</param> /// <param name="arrayIndex">Index of the array.</param>
public void CopyTo(string[] array, int arrayIndex) => Array.Copy(key.GetValueNames(), 0, array, arrayIndex, Count); public void CopyTo(string[] array, int arrayIndex) { if (key != null) Array.Copy(key.GetValueNames(), 0, array, arrayIndex, Count); }
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose() => key?.Close();
/// <summary>Gets the enumerator.</summary> /// <summary>Gets the enumerator.</summary>
/// <returns></returns> /// <returns></returns>
public IEnumerator<string> GetEnumerator() => key.GetValueNames().Cast<string>().GetEnumerator(); public IEnumerator<string> GetEnumerator() => (key?.GetValueNames().Cast<string>() ?? new string[0]).GetEnumerator();
/// <summary>Removes the specified item.</summary> /// <summary>Removes the specified item.</summary>
/// <param name="item">The item.</param> /// <param name="item">The item.</param>