c# - 在链接它们搜索 key 时如何正确关闭RegistryKey?

标签 c# registry idisposable

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

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 中执行此操作,但我很确定打开新 key 算作分配,这意味着 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;
}

该类还可以进行一些扩展,以便在并发情况下工作(例如使用 ConcurrentBagConcurrentQueue),并且可以针对其他更具体的需求进行扩展(就像 RegistryKey 的集合),如果您发现自己在多个地方需要此逻辑,这将很有用。

关于c# - 在链接它们搜索 key 时如何正确关闭RegistryKey?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42558171/

相关文章:

io - 在 F# 中正确处理流

c# - 在连接之间共享事务

windows - 用于插入多个注册表项并将每个记录结果记录到文件的批处理文件

c# - 使 SortableBindingList 使用稳定排序的最简单方法

windows - 如何以编程方式为某些 USB 设备禁用 "Enhanced Power Management"?

javascript - 在 HTA 文件中使用 Javascript 从 Windows 注册表读取/写入

c# - 为什么这些对象没有被处理掉

c# - 您是否应该实现 IDisposable.Dispose() 以使其永远不会抛出?

c# - 使用 LINQPad 查询包含嵌套元素的 XML 文件?

c# - 从 X509 Cert pfx 文件中获取字符串