Vista + VB.NET - Access Denied while writing to HKEY_LOCAL_MACHINE

前端 未结 3 1154
挽巷
挽巷 2021-01-23 21:39

I want my program to be able to edit a values within a registry key that resides in \'HKEY_LOCAL_MACHINE\'

    My.Computer.Registry.SetValue(\"HKEY_LOCAL_MACHINE         


        
相关标签:
3条回答
  • 2021-01-23 21:51

    Since changes to the Local Machine hive can have affect across the system you will find that changes to it are restricted to non-administrative users. If you try to run your same code on an XP machine a non-admin account you will get the same error. On Vista since the process is non-admin by default you are getting this error. Information on this is in the "Made for Windows 2000", "Made for Windows XP", and "Made for Windows Vista" certification guidelines.

    Given that I've got little information on what your program is doing more information may be needed to give you specific guidance, so I will speak in the general case. You want your application to leave the local machine hive alone unless you need it. When your program is run in non-admin mode it you can either disable the functionality that requires access to these admin keys or you can request that the admin privs.

    0 讨论(0)
  • 2021-01-23 21:59

    You are running into Vista's UAC feature. It will not let you write to arbitrary places in the HKLM hive because you are not running with Administrative priviledges.

    There are two ways to work around this issue

    1. Run the program with Administrative priviledges (different than running a program as an account which has Administrative priviledges)
    2. Choose another place, perhaps HKCU, to store the data

    The second option is much better as it allows your application to run with non-Admin priviledges which you can't always assume your user has.

    Here is a fairly detailed article on UAC. It's not 100% programming material but it gives a good explanation as to what exactly it is and you can hopefully relate that to your particular program: http://technet.microsoft.com/en-us/library/cc709691.aspx

    0 讨论(0)
  • 2021-01-23 22:02

    Vista has tighter restrictions around Adminstrator accounts. If you're not logged in as an Administrator account, you'll have to write to HKEY_CURRENT_USER as opposed to HKEY_LOCAL_MACHINE.

    IMO, this makes more sense. Each user has their own settings/etc for their programs. If you want to make global settings for your program, you'll have to do it with an Administrator account.

    0 讨论(0)
提交回复
热议问题