To request elevation from Windows operating system, you have to include a manifest into your application:
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- Leave the desired execution level here -->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false">
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
The execution levels are defined as follows (taken from MSDN here):
asInvoker: The application will run with the same permissions as the process that started it. The application can be elevated to a higher permission level by selecting Run as Administrator.
highestAvailable: The application will run with the highest permission level that it can. If the user who starts the application is a member of the Administrators group, this option is the same as requireAdministrator. If the highest available permission level is higher than the level of the opening process, the system will prompt for credentials.
requireAdministrator: The application will run with administrator permissions. The user who starts the application must be a member of the Administrators group. If the opening process is not running with administrative permissions, the system will prompt for credentials.
Conclusion
To write values to the registry, you should probably include <requestedExecutionLevel level="requireAdministrator" />
in your manifest.
But perhaps you shouldn't even write to HKEY_LOCAL_MACHINE
but HKEY_CURRENT_USER
(please refer to this answer).