2

I have this code but I don't seem to understand exactly the purpose of the permission Demand method

 RegistryPermission registryPermission = new RegistryPermission(RegistryPermissionAccess.AllAccess, @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
registryPermission.Demand();

RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
if (checkBoxLoadStartup.Checked)
{
     //make an entry in the registry to make this program run at start up
     regKey.SetValue(Application.ExecutablePath.ToString(), Application.ExecutablePath.ToString());
}
else
{
    //delete the entry
    regKey.DeleteValue(Application.ExecutablePath.ToString());
}

I expected to see a window popping up and asking me to allow the permissions to write the registry. Instead I got an exception. To make it work I added:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

in app.manifest and now I don't see any popups but I'm allowed to change the values.

Is it possible to show a popup asking a question to give permissions to change the registry and based on the given permissions to modify or not the registry key?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.