简体   繁体   中英

How to launch app at start as administrator?

My problem is simple. I can't find both problem's solutions together. My question about launch my program at startup with admin permission without warning. I just want to get admin rights on setup. I'm using regedit for launching my program at startup its working without admin rights. If i try to give admin rights on app manifest, program isn't launching at startup. How should i solve this ? Also program need a gui so i can't use services. Thanks for the help.

There are my startup codes

private void SetStartup()
    {
        RegistryKey rk = Registry.CurrentUser.OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

        if (chkStartUp.Checked)
        {
            rk.SetValue("_connectorEthernet", Environment.CurrentDirectory + @"\_connectorEthernet");
            WriteToSettingFile("true");
        }
        else
        {
            rk.DeleteValue("_connectorEthernet", false);
            WriteToSettingFile("false");
        }
    }

Ok, i think i solved. Firstly i made

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

For the program launchs at startup. If you dont do this program doesn't start.

After that i used settings.setting in solution explorer window.

settings.setting

This is for getting settings

chkStartUp.Checked = Properties.Settings.Default.chk;

And this is for the change and save settings

Properties.Settings.Default.chk = true;
Properties.Settings.Default.Save();

And lastly this code for run cmd as administrator and getting respond.

        var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {

                FileName = "netsh.exe",
                Arguments = "lan reconnect",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
                Verb = "runas"
            }
        };
        proc.Start();
        string line = proc.StandardOutput.ReadToEnd();
        MessageBox.Show(line);

This is how i solved my problems. I hope it might be helpfull others. If you have any other idea to do that please contact me.

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