0

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");
        }
    }
  • What is the warning you are referring to when writing "_startup with admin permission without warning_"? Do you mean the Windows dialog informing you that you are about to start a program with admin previleges (and possibly asking you about admin credentials)? –  Nov 02 '18 at 22:59
  • 1
    If you have a program requiring/requesting privilege elevation and you are not using an elevated/privileged account (administrator) to start this program, Windows will request user confirmation (and possibly request admin credentials). There is no way around it. For a program to be able to "silently" obtain or run itself with admin permissions without requiring at least user confirmation/authentification (or other security checks) when started from a normal/restricted user account is the wet dream of every single malware programmer... –  Nov 02 '18 at 23:03
  • Think about it. If any app could include code that forced the app to start as admin, then the whole infrastructure that keeps apps from running as admin would be useless. All the evil apps would just make that demand. The easiest way to get your app to require admin rights (which may prevent non-admin users from using it) is to include that demand in a _manifest_ embedded in your application: https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator. Look up information about _User Account Control_ – Flydog57 Nov 03 '18 at 00:04
  • Firstly thanks for the answer. I would like to make a program it launchs at startup. It need to config itself on text configuration file(its in program's setup directory on Local C:) and if any setting changes write over the text file. After that it needs to runs some cmd code in background. That codes about network proccess and needs to admin perms. The main idea is i want to connect my school network with ethernet easly. That needs some configuration on network card. There is already a program for it but it has some bugs. I want to change it with mine. I hope it's clearly enough now. – Gökhan Gökce Nov 03 '18 at 16:26
  • Warning because of `` – Gökhan Gökce Nov 03 '18 at 16:29

1 Answers1

0

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.