vs2008, c#: First time my winforms app runs it needs to create a setting in HKLM\Software. Obviously a permissions problem, but the requirement is that the user may not have admin rights, and setting permissions on the registry manually is not possible for the end user. I could create keys during install, but is there any way to do this from the program?
RegistryKey key = Registry.LocalMachine.CreateSubKey(@"Software\MyCompany\MyApp 1.0");
Just testing the app from vs2008 gives this error: Access to the registry key 'HKEY_LOCAL_MACHINE\Software\MyCompany\MyApp' is denied.
If I run it as开发者_运维技巧 administration no errors.
The only way to do it from your program is to execute the code that writes to the registry under an account that has sufficient permissions. You may check out this article which gives an example of impersonating a user. There are some security considerations to take into account with this method so IMHO the best way is to perform this operation during program install.
This is the UAC of Win7. If you want to access registry keys you'll have to set either in the manifest that the programm needs admin privileges or easily delete this. So you can write
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
for admin rights into the app.manifest in your properties folder or you write
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
</requestedPrivileges>
</security>
</trustInfo>
If you don't want to use admin rights.
精彩评论