链接它们以搜索密钥时如何正确关闭 RegistryKey?

How to properly close RegistryKey when chaining them to search for key?

我正在使用以下方法在注册表中搜索键和 return 它的值。我传入要搜索的根键和目标的相对路径作为字符串。在解析路径时,我使用上一个密钥作为打开下一个级别的基础。我不是严格只读的,所以在我完成后是否有必要关闭基本密钥?如果是这样,是这样做的方法吗?

public static string QueryRegistry (RegistryKey root, string path)
{
    return path.Split(Path.DirectorySeparatorChar)
    .Aggregate(root, (r, k) =>
    {
        var key = r?.OpenSubKey(k);
        r?.Close();
        return key;
    }).GetValue(null).ToString();
}

所以这里的基本问题是您正在创建不确定数量的 IDisposable 对象,并且您不确定何时准备好关闭它们。在这种情况下,我所做的是创建一个集合来跟踪它们,然后在我完成后处理集合中的所有内容。不过,这里存在一些您无法正确处置对象的风险。

public static string QueryRegistry (RegistryKey root, string path)
{
    List<IDisposable> resourceTracker = new List<IDisposable>() { root };
    string ret = null;
    try 
    {
        ret = path.Split(Path.DirectorySeparatorChar)
        .Aggregate(root, (r, k) =>
        {
            var key = r?.OpenSubKey(k);
            if (key != null) 
            {
                resourceTracker.Add(key);
            }
            return key;
        }).GetValue(null).ToString();
    }
    finally
    {
        foreach (var res in resourceTracker)
        {
            res.Dispose();
        }
    }
    return ret;
}

您可以尝试在 CER 中执行此操作,但我很确定打开新密钥算作分配,这意味着 CLR 无论如何都不会将其视为 CER。但这应该是足够安全的。

这可以抽象成一个集合(有点像 rory.ap 建议的)像这样:

public class DisposableCollection : IList<IDisposable>, IDisposable
{
    private List<IDisposable> disposables = new List<IDisposable>();

    #region IList<IDisposable> support

    public int Count
    {
        get
        {
            return ((IList<IDisposable>)disposables).Count;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return ((IList<IDisposable>)disposables).IsReadOnly;
        }
    }

    public int IndexOf(IDisposable item)
    {
        return ((IList<IDisposable>)disposables).IndexOf(item);
    }

    public void Insert(int index, IDisposable item)
    {
        ((IList<IDisposable>)disposables).Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        ((IList<IDisposable>)disposables).RemoveAt(index);
    }

    public void Add(IDisposable item)
    {
        ((IList<IDisposable>)disposables).Add(item);
    }

    public void Clear()
    {
        ((IList<IDisposable>)disposables).Clear();
    }

    public bool Contains(IDisposable item)
    {
        return ((IList<IDisposable>)disposables).Contains(item);
    }

    public void CopyTo(IDisposable[] array, int arrayIndex)
    {
        ((IList<IDisposable>)disposables).CopyTo(array, arrayIndex);
    }

    public bool Remove(IDisposable item)
    {
        return ((IList<IDisposable>)disposables).Remove(item);
    }

    public IEnumerator<IDisposable> GetEnumerator()
    {
        return ((IList<IDisposable>)disposables).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IList<IDisposable>)disposables).GetEnumerator();
    }

    public void AddRange(IEnumerable<IDisposable> range)
    {
        disposables.AddRange(range);
    }

    public IDisposable this[int index]
    {
        get
        {
            return ((IList<IDisposable>)disposables)[index];
        }

        set
        {
            ((IList<IDisposable>)disposables)[index] = value;
        }
    }
    #endregion

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls


    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                foreach(var disposable in disposables)
                {
                    disposable.Dispose();
                }
            }

            // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
            // TODO: set large fields to null.

            disposedValue = true;
        }
    }

    // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
    // ~DisposableCollection() {
    //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    //   Dispose(false);
    // }

    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
        // TODO: uncomment the following line if the finalizer is overridden above.
        // GC.SuppressFinalize(this);
    }


    #endregion
}

然后可以这样使用:

public static string QueryRegistry (RegistryKey root, string path)
{
    string ret = null;
    using (var resourceTracker = new DisposableCollection() { root })
    {
        ret = path.Split(Path.DirectorySeparatorChar)
        .Aggregate(root, (r, k) =>
        {
            var key = r?.OpenSubKey(k);
            if (key != null) 
            {
                resourceTracker.Add(key);
            }
            return key;
        }).GetValue(null).ToString();
    }
    return ret;
}

class 也可以扩展一点以在并发情况下工作(比如使用 ConcurrentBagConcurrentQueue),并且可以扩展以满足其他更具体的需求(像 RegistryKeys 的集合),如果你发现自己在多个地方需要这个逻辑,这将很有用。