Search code examples
c#windows-10uacadmin-rights

How to autorun on system start with admin ritghts from manifest? c#


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.


Solution

  • 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"