Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a c# program that it runs on startup
I'm going to run this program as administrator on startup
Please help me

What I have tried:

I have tried this code in app.manifest file to run the program as administrator but program doesn't run on startup :
C#
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Posted
Updated 19-Dec-19 6:28am

If you put a shortcut in the "startup" folder and set the application to run as administrator, it won't work. Windows will block the application from running, and show a tray icon to allow the user to start the program.

You'll need to use the Windows Task Scheduler to launch your program instead:
Make Vista launch UAC restricted programs at startup with Task Scheduler - TechRepublic[^]
 
Share this answer
 
Comments
Mohammad Tavoosi 19-Dec-19 12:20pm    
I put exe file address to Registry and it works with sample program but Does not work with run program as administrator.
please help me
Richard Deeming 19-Dec-19 12:25pm    
As I said, Windows will block startup programs from running elevated. You're going to have to use the Task Scheduler.
In addition to Richard's advice, here's a trick I used to allow an application to start at startup:
In your application check if it is run with admin rights, if not then restart it asking for admin rights.
At the moment I do not have my source code at hand, but I will post it tomorrow if you want it.
// Program

        public static bool IsAdministrator()
        {
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }


// Form1

        private void Form1Shown(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.Run1();
            this.Cursor = Cursors.Default;
        }

        private void Run1()
        {
            if (!Program.IsAdministrator())
            {
                // Restart and run as admin
                var exeName = Process.GetCurrentProcess().MainModule.FileName;
                ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
                startInfo.Verb = "runas";
                startInfo.Arguments = "restart";
                Process.Start(startInfo);
                Application.Exit();
            }
		}
 
Share this answer
 
v3
Comments
Mohammad Tavoosi 19-Dec-19 12:27pm    
thanks
RickZeeland 20-Dec-19 1:56am    
Updated with the source code as promised :)
Also one option is to start new process with elevated privileges. Have a look at the example in Option 2 in the beginning of Directory size browser[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900