-1

I have the problem that my application doesn't start on system start anymore after I added this to the manifest

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

I know that it has something to do with the Windows UAC. But I dont really know how to solve this without losing admin rights on program start.

This is how I write into registry:

 if (checkBox13.Checked)
            {
                File.WriteAllText(@"C:\ProgramData\Programname\autorunstate", "true");

                RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                rk.SetValue("Programname", Application.ExecutablePath.ToString());

            }

Would be really nice if someone could tell me how to let the program start on system start now? It worked before I added this line into manifest. Thank you.

Sardar Agabejli
  • 423
  • 8
  • 32
  • 1
    When you require administrative privileges, there must be a logged in user to provide those privileges. When you execute at system startup, there is no user yet to provide them. Chicken/egg: Your app requires admin user's permission to allow elevation, but you want to run before an admin user is available to provide permission. Why specifically do you need to run elevated (other than to get the privileges needed to write to the registry key)? – Ken White Jun 20 '17 at 17:13
  • But it starts after I am logged into windows. So how to let it run after a user is logged in? – Sardar Agabejli Jun 20 '17 at 17:15
  • Use HKCU instead of HKLM, or create a shortcut in the user's Startup folder (on Win7, that's in C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup). – Ken White Jun 20 '17 at 17:17
  • But I am still using "current user" and not "local machine". `RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);` – Sardar Agabejli Jun 20 '17 at 17:22
  • Windows forbids programs from elevating themselves at login time. It would be *drastically* confusing to the user when they get the consent prompt immediately after logging in, they'll have about no idea what they are asked to approve. You'll have to consider two separate programs, the one you want to get started and another that tinkers with the Windows configuration. Like an installer, this is the kind of job that Setup.exe ought to do. – Hans Passant Jun 20 '17 at 17:25
  • This means that another program that is able to launch from startup can start my application wich will have admin rights? – Sardar Agabejli Jun 20 '17 at 17:30
  • 1
    Repeat of earlier question: Why do you need to run elevated? – Bill_Stewart Jun 20 '17 at 22:28
  • Because I want to write to files in "C:\ProgramData" and I get Exceptions when I do that without getting admin rights on program start from manifest. – Sardar Agabejli Jun 21 '17 at 11:55
  • 2
    Set the permissions where you want to write at installation time when you are elevated. Then when your program runs, you don't need elevation. – Bill_Stewart Jun 21 '17 at 13:01

1 Answers1

0

OK, I solved this problem by creating the folder I want to write in the ProgramData Folder on installation. I used Visual Studio Setup Projects, now I can write into /ProgramData/ProgramName/ folder. Thank you for your help.

"Set the permissions where you want to write at installation time when you are elevated. Then when your program runs, you don't need elevation"

Sardar Agabejli
  • 423
  • 8
  • 32