简体   繁体   English

如何以管理员身份启动应用程序?

[英]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. 我正在使用regedit在启动程序时启动我的程序,而没有管理员权限。 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. 另外程序需要一个GUI,所以我不能使用服务。 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。

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. 最后,此代码用于以管理员身份运行cmd并获得响应。

        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. 如果您还有其他想法,请与我联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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