通过 C# 在 Windows 注册表中创建一个 REG_NONE 空值

Create a REG_NONE empty value in the Windows Registry via C#

如何在 Windows 注册表中创建类型为 REG_NONE 的空值?

例如,对于这个键:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\
  .htm\OpenWithProgids

这是我在 Windows 7 机器上看到的值:

----------------------------------------------------
Name           Type       Data
----------------------------------------------------
(Default)      REG_SZ     (value not set)
FirefoxHTML    REG_NONE   (zero-length binary value)
htmlfile       REG_NONE   (zero-length binary value)
----------------------------------------------------

具体来说,我如何声明和初始化类型为 "zero-length binary value" 的变量,以便将其传递给 RegistryKey.SetValue? Note that I could not find the help that I needed in the documentation for the RegistryValueKind 枚举。

[这是我的研究结果。]

我在 Microsoft 的 RegistryKey.cs:

参考资料中找到了我正在寻找的提示
byte[] value = new byte[0];

具体来说,调用 RegistryKey.SetValue 其中:

  • name 设置为您想要的密钥名称,
  • value 设置为 empty byte array,并且
  • valueKind 设置为 RegistryValueKind.None
 using Microsoft.Win32;

 RegistryKey key; // set via 'CreateSubKey' or 'OpenSubKey'
 key.SetValue("KeyName", new byte[0], RegistryValueKind.None);

但是,请注意,至少对于出现在 OpenWithProgids 键中的值,Windows 似乎也将 空字符串 值视为等效值:

 key.SetValue("KeyName", string.Empty, RegistryValueKind.String);

例如,对于这个键:

HKEY_CLASSES_ROOT\.vcxproj\OpenWithProgids

这是我在我的机器上看到的值:

---------------------------------------------------------------
Name                                  Type      Data
---------------------------------------------------------------
(Default)                             REG_SZ    (value not set)
Expression.Blend.vcsproj.12.0         REG_SZ
VisualStudio.Launcher.vcxproj.10.0    REG_SZ
VisualStudio.Launcher.vcxproj.12.0    REG_SZ
VisualStudio.vcxproj.10.0             REG_SZ
VisualStudio.vcxproj.12.0             REG_SZ
---------------------------------------------------------------

相关:参见this answer and 示例,了解如何将类型RegistryValueKind.Binary的注册表值的数据设置为非-空字节数组。