处置RegistryKey也会将其关闭吗? [英] Will disposing a RegistryKey also close it?

查看:43
本文介绍了处置RegistryKey也会将其关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述.

另外,反之亦然.将关闭RegistryKey进行处理吗?

我浏览了所有可以找到的文档,并且在任何地方都没有提及.

解决方案

它将在 Close()内部调用 Dispose()方法,表示将会是"已处置",而围绕 Dispose()的其他方式将是 Close() key .系统注册表项永远不会关闭.仅已关闭- HKEY_PERFORMANCE_DATA .

.net的 Close 方法的源代码:

 /***关闭此键,如果内容已修改,则将其刷新到磁盘.*/public void Close(){处置(true);} 

.NET源行220

.Dispose 方法的

.NET源:

  [System.Security.SecuritySafeCritical]//自动生成私人空白Dispose(布尔处置){如果(hkey!= null){如果(!IsSystemKey()){尝试 {hkey.Dispose();}捕获(IOException){//现在我们并不在乎句柄是否无效}最后{hkey = null;}}否则if(dispose&& IsPerfDataKey()){SafeRegistryHandle.RegCloseKey(RegistryKey.HKEY_PERFORMANCE_DATA);}}} 

.NET源行227

As the title says.

Also, the other way around; will closing a RegistryKey dispose it?

I've looked around in all documentation I could find and nothing of this is mentioned anywhere.

解决方案

It will call the Dispose() method inside of Close(), meaning YES it will be "disposed" and other way around Dispose() will Close() key. System registry keys are never closed. Only keys that are being closed - HKEY_PERFORMANCE_DATA.

.NET source for Close method:

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

.NET source line 220

.NET source for Dispose method:

[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 source line 227

这篇关于处置RegistryKey也会将其关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆