Search code examples
c#registry

Convert registry entry from REG_SZ to REG_DWORD?


I've inherited an application with registry entries like "AutoLogOff" (and many similar items) as a REG_SZ type but it only has values of "0" or "1". It would be more logical for this to be numerical (ie. a DWORD). Is there a simple way to convert an existing REG_SZ entry to a REG_DWORD (change type)?

I suppose I could detect "upgrade from old version", extract the REG_SZ value and delete the existing registry key, then re-create the registry key with the same name but as a REG_DWORD type...but it seems like there might be an easier way. Using C# here, though it is the principle I'm interested in.


Solution

  • There is no "easy way", it requires deleting the value and adding it back.

    This is not a fantastic idea, if the user ever restores an older version of the app for whatever reason (it does happen a lot more often than you might think) then the old version is very likely to misbehave, not limited to a hard crash. So do not try to change the type, simply use a value with another name. Like "AutoLogOff2". Eventually machines with the old name will disappear. The effort of creating the new value is otherwise the same. Yuck factor is high.