You need administrator privileges for writing to much of the registry. Check that your application has the correct settings in its manifest, or run as administrator.
Add the following to your manifest:
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='requireAdministrator' uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>