Registry SetValue stores double as REG_SZ

  • Thread starter Thread starter ChuckD_Duncan
  • Start date Start date
C

ChuckD_Duncan

using Microsoft.Win32;
RegistryKey key = Registry.LocalMachine;
key = key.OpenSubKey(".....");
...
key = key.OpenSubKey("PCF", true);
double aDoubleValue = 0.25;
key.SetValue("testkey", aDoubleValue);

testkey pre-existed as a DWORD value and after the setvalue becomes a
REG_SZ type. Even tried casting the aDoubleValue, but no change.

Whats wrong with SetValue?

Thanks in advance.
Chuck
 
Hello Chuck,
testkey pre-existed as a DWORD value and after the setvalue becomes a
REG_SZ type. Even tried casting the aDoubleValue, but no change.

The native types for registry values are strings, binary values and DWORDs
(integers). There is no native type for storing a floating point value. If
you give the SetValue method a double value, it gets converted to a string.
So, the method works as expected.

You should either round or convert the value to an Int32 first, or store it
as a binary value if a string won't do. For example:

--------------------------------
Microsoft.Win32.RegistryKey key =
Registry.CurrentUser.OpenSubKey("Software\\ACME",true);
double myValue = 123.45;
key.SetValue("My Value",System.Convert.ToInt32(myValue));
key.Close();

--------------------------------

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Back
Top