4

I have following C# code, which reads the UAC state from registry in Windows 7

object obj = Registry.LocalMachine.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA", (long)1);

It works perfectly on Windows 7 with admin/not-admin accounts. It always returns the default value I provide it under Windows 8. The registry key is there. I can see its value with regedit. But the C# code does not read it. Can anybody tell why? It is a .net 4 application. The user account is unelevated admin.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Ivan Milanov
  • 69
  • 1
  • 6
  • 3
    What does "does not read it" mean? Any exception, unexpected result...? – ken2k Mar 06 '13 at 10:54
  • Sounds like it's being blocked on purpose by the OS. Have you requested permission to access that part of the registry in your code? – James Snell Mar 06 '13 at 11:14
  • This will help you. see http://stackoverflow.com/questions/12298199/c-sharp-read-registry-windows-8 – Ravi Patel Mar 06 '13 at 11:15
  • Always use manifest if you want to access classesroot or localmachine registry hive. http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7/2818776#2818776 – Ravi Patel Mar 06 '13 at 11:17
  • "does not read" means it always returns the default value I give as a parameter. I have not requested permission to access the path. About employing manifest file: This program is supposed to check if there is need of a new version update over internet. If yes then another process is started. It replaces files in "Program Files" folder and it uses manifest file. Before starting this process I need to check if the running user is not administrator and if UAC is off. In this case the replacing files program will fail and this is why I read that registry value. – Ivan Milanov Mar 08 '13 at 20:03
  • Why do you need to read that registry key? Why not just check whether the current process is running elevated? – Bill_Stewart Mar 12 '13 at 15:41
  • Most likely this is an x86, x64 registry redirection issue. – David Heffernan Aug 25 '13 at 20:31

2 Answers2

4

It returns the default value (on both Windows 7 and 8). Here's the code that reads the correct Registry value on both Windows 7 and 8, without running as administrator.

        object obj = Registry.GetValue( 
             @"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System\", 
             "EnableLUA", 
             (long)1);

Note that we call GetValue method on Registry, not on Registry.LocalMachine, and we pass key and value name as two separate parameters.

regmagik
  • 574
  • 3
  • 14
-1

You need to require administrative privileges at least to be able to access the registry by default. Windows 8 developers possibly thinks it's safer to keep it that way.

  • 2
    No you do not need admin rights to read the registry. If you did, how would anything work? Just imagine a world where a non-admin user could not read the registry. – David Heffernan Aug 25 '13 at 20:29
  • More and more is see stupid nonsense answers here on StackOverflow from users who have 1 point of reputation. They are like spammers here. – Elmue Jul 09 '15 at 16:21