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

时间:2016-12-22 18:33:31

标签: c# .net registry nonetype registrykey

如何在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)
----------------------------------------------------

具体来说,如何声明和初始化“零长度二进制值”类型的变量,以便将其传递给RegistryKey.SetValue?请注意,我在RegistryValueKind枚举的文档中找不到我需要的帮助。

1 个答案:

答案 0 :(得分:4)

[这是我研究的结果。]

我在Microsoft的RegistryKey.cs参考源中找到了我正在寻找的提示:

byte[] value = new byte[0];

具体来说,请致电RegistryKey.SetValue,其中:

 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
---------------------------------------------------------------

相关:有关如何将RegistryValueKind.Binary类型的注册表值的数据设置为非的示例,请参阅this answerthis answer字节数组。