问题
How can I create an empty value in the Windows Registry that has the type REG_NONE
?
For instance, for this key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\
.htm\OpenWithProgids
Here are the values that I see on my Windows 7 machine:
----------------------------------------------------
Name Type Data
----------------------------------------------------
(Default) REG_SZ (value not set)
FirefoxHTML REG_NONE (zero-length binary value)
htmlfile REG_NONE (zero-length binary value)
----------------------------------------------------
Specifically, how do I declare and initialize a variable of type "zero-length binary value" for the purpose of passing it to RegistryKey.SetValue? Note that I could not find the help that I needed in the documentation for the RegistryValueKind enumeration.
回答1:
[Here is the result of my research.]
I found the hint I was looking for in Microsoft's reference source for RegistryKey.cs:
byte[] value = new byte[0];
Specifically, call RegistryKey.SetValue where:
name
is set to the name you want for the key,value
is set to an empty byte array, andvalueKind
is set to RegistryValueKind.None.
using Microsoft.Win32;
RegistryKey key; // set via 'CreateSubKey' or 'OpenSubKey'
key.SetValue("KeyName", new byte[0], RegistryValueKind.None);
However, note that at least for values that appear in an OpenWithProgids
key, Windows also appears to treat empty string values as equivalent:
key.SetValue("KeyName", string.Empty, RegistryValueKind.String);
For instance, for this key:
HKEY_CLASSES_ROOT\.vcxproj\OpenWithProgids
Here are the values that I see on my machine:
---------------------------------------------------------------
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
---------------------------------------------------------------
Related: See this answer and this answer for examples for how to set the data of a Registry value of type RegistryValueKind.Binary
to a non-empty byte array.
来源:https://stackoverflow.com/questions/41289666/create-a-reg-none-empty-value-in-the-windows-registry-via-c-sharp