So I’m writing some code and using RegistryKey.GetValue and I can’t seem to get it to return the proper string array. I would use RegSaveKeyEx except I need to change the location of where the keys are being saved to in the reg file I’m creating.
The MultiString I’m querying has two lines in it, : and a blank line. Regedit gives hex(7):3a,00,00,00,00,00 for the reg file export of said value. This is correct. 3a would be :, then a null, two nulls for the blank line and finally two more nulls to signify the end of the value. My code is giving hex(7):3a,00,00,00, which would be a : and the double-null EOL characters on a MultiString. When imported back into the registry hex(7):3a,00,00,00 is only a :, the blank line is missing.
If I import hex(7):3a,00,00,00,00,00 then view the value in regedit, I see : and a blank line. This is correct and this is what I want my code to do.
When I query this value with RegistryKey.GetValue in C# 4.0 I get the a string[] with just one element, the :.
For other MultiString values I get an array containing an element for every line, as I would expect.
RegistryKey.GetValue doesn’t return anything for the blank line. It doesn’t return a null, a blank string in the array, nothing.
On everything except blank lines it returns exactly the same result as regedit export. What needs to be changed to have it return an array that includes the blank line?
Here’s my code if that’s helpful:
private static string RegMultiStringExtraction(RegistryKey UsersSubKey, string Key)
{
string[] InputStrs = (string[])UsersSubKey.GetValue(Key, Environment.NewLine, RegistryValueOptions.DoNotExpandEnvironmentNames);
StringBuilder sb = new StringBuilder();
foreach (string str in InputStrs)
{
char[] Values = str.ToCharArray();
foreach (char letter in Values)
{
int ValueInt = Convert.ToInt32(letter);
sb.Append(ValueInt.ToString("x"));
sb.Append(",00,");
if (Values.Length == 0)
{
if (sb.Length == 0)
{
sb.Append("00,");
}
else if (sb.Length > 1)
{
sb.Append(",00,");
}
}
}
sb.Append("00,00,");
}
string Value = ('"' + Key.ToString() + @"""=" + "hex(7):" + sb.ToString().TrimEnd(','));
return Value;
}
Double null-terminated strings cannot contain empty lines. An attempt to include empty lines means including two consecutive nulls, and that is interpreted as termination rather than an empty line. Raymond Chen discussed this quirk much more eloquently that I can.
Perhaps you can save to
REG_SZand useEnvironment.NewLineas a delimiter.