-2

I already took a look at threads like this: Disabling UAC programmatically However they are kinda old and it seems I can't do it. I'm trying to create a Windows tool to help the user doing certian process (like enabling or disabling UAC). Here is my code so far it won't turn down or up the UAC value even with admins right. Right now I'm not using the admin account (Windows 10) but I do have access to the local admin credetial.

try
        {
            RegistryKey uac = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
            if (uac == null)
            {
                uac = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");

            }
            uac.SetValue("EnableLUA", 0);
            uac.Close();
        }
        catch (Exception)
        {
            MessageBox.Show("Error!");
        }
TheNoobUser
  • 406
  • 1
  • 5
  • 17
  • _"it has failed"_ - please read [ask] and elaborate. Start by removing the `catch (Exception)`, as that hides essential information. – CodeCaster Jan 07 '19 at 11:04
  • I'm curious: why do you need to do this? – AKX Jan 07 '19 at 11:06
  • @CodeCaster By removing the try catch without admins right, the application just crash saying it has no right to access the reg key. with admins right, no crahes or anything but it not disabling or enabling the UAC. Also I'm doing it just to learn coding, and I though a Windows tool would be a good idea – TheNoobUser Jan 07 '19 at 11:09
  • You are not supposed to be able do that programmatically (except in old/broken (vista) windows versions) – EpicKip Jan 07 '19 at 11:20
  • @EpicKip I have a software that turns down the value of the UAC and than ask the user to restart the computer saying that changes to UAC were made. My idea is to replicate that function. My intention is not to do it hiding it from the user – TheNoobUser Jan 07 '19 at 11:22
  • @TheNoobUser And that program works in windows 10? – EpicKip Jan 07 '19 at 11:23
  • @EpicKip Yes sir it does. – TheNoobUser Jan 07 '19 at 11:23
  • @TheNoobUser Ok did you try to run your program as admin (also logged in as local admin) – EpicKip Jan 07 '19 at 11:25
  • @EpicKip Not logged in as admin, only with admins right. I will give it a shot now – TheNoobUser Jan 07 '19 at 11:27
  • @EpicKip Not working either, I'll try what Gaurav suggested now. thanks for the help – TheNoobUser Jan 07 '19 at 12:51

1 Answers1

1

You are using Registry.CurrentUser here, please use LocalMachine instead. So the code would be something like this:

RegistryKey uac = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
                if (uac == null)
                {
                    uac = Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");

                }
                uac.SetValue("EnableLUA", 0);
Gaurav
  • 782
  • 5
  • 12
  • The code I gave you works fine on my machine so what's the issue with it on your machine, just that you need admin rights with it. As you said in your comments you just need to restart after running this code for implementation. – Gaurav Jan 07 '19 at 12:36
  • This code worked just fine! Thanks a lot! I guessed the current user was working just fine, but I was wrong. Cheers! – TheNoobUser Jan 07 '19 at 12:55