释放 RegistryKey 也会关闭它吗?

12

正如标题所说。

另外,反过来也是一样的吗?关闭一个RegistryKey会自动释放它吗?

我查看了所有可以找到的文档,但没有任何地方提到这些。

1个回答

13

它将在Close()内部调用Dispose()方法,这意味着YES它将被“disposed”,反之Dispose()将关闭key。系统注册表键永远不会closed。只有正在closedkeys - HKEY_PERFORMANCE_DATA

Close方法的.NET源代码:

/**
 * Closes this key, flushes it to disk if the contents have been modified.
 */
public void Close() {
    Dispose(true);
}

.NET源代码第220行

Dispose方法的.NET源代码:

[System.Security.SecuritySafeCritical]  // auto-generated
private void Dispose(bool disposing) {
      if (hkey != null) {
           if (!IsSystemKey()) {
              try {
                  hkey.Dispose();
                }
                catch (IOException){
                    // we don't really care if the handle is invalid at this point
                }
                finally
                {
                    hkey = null;
                }
            }
            else if (disposing && IsPerfDataKey()) {
            SafeRegistryHandle.RegCloseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
            }  
      }
}

.NET源代码第227行


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接