简体   繁体   中英

Application add to startup ( need administrator rights to start app )

I have a C# application and I want to add this application to start-up. I use:

 RegistryKey Key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
 Key.SetValue("APPName", "C:\\Program Files\\App\AppName.exe");

My application need Administrator Right to run, I want a method to register my app or to add to trusted applications.

Thanks.

Now I'm using:

        var info = new ProcessStartInfo(
            Assembly.GetEntryAssembly().Location)
        {
            Verb = "runas", // indicates to elevate privileges
        };

        var process = new Process
        {
            EnableRaisingEvents = false, // enable WaitForExit()
            StartInfo = info
        };

        process.Start();

For obtain access only for one method, but now I have problem with

     unauthorizedAccessException was unhandled

I'm trying to create a directory in C:\\Program Files.

If your using visual studio 2010 or 2012, goto add new item, Application Manifest. Change the requestedExecutionLevel to this below:

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

Now when your application starts it will ask to run in elevated mode.

Another way is to restart the application in elevated mode. For this you don't need the app manifest.

You can read about both these techniques here:
http://www.codeproject.com/Articles/105506/Getting-Elevated-Privileges-on-Demand-using-C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM