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)
{
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>
@ -120,8 +121,8 @@ namespace Vanara.Windows.Shell.Registration
{
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));
using var sk = ShellRegistrar.GetRoot(systemWide, true);
try { sk.DeleteSubKeyTree(extension); } catch { sk.DeleteSubKey(extension, false); }
using (var sk = ShellRegistrar.GetRoot(systemWide, true))
try { sk.DeleteSubKeyTree(extension); } catch { sk.DeleteSubKey(extension, false); }
ShellRegistrar.NotifyShell();
}

View File

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