c# - 如何以管理员身份启动应用程序?
问题描述
我的问题很简单。我无法同时找到这两个问题的解决方案。我关于在没有警告的情况下以管理员权限启动我的程序的问题。我只想获得设置的管理员权限。我正在使用 regedit 在启动时启动我的程序,它在没有管理员权限的情况下工作。如果我尝试在应用清单上授予管理员权限,则程序不会在启动时启动。我应该如何解决这个问题?程序还需要一个 gui,所以我不能使用服务。谢谢您的帮助。
有我的启动代码
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");
}
}
解决方案
好的,我想我解决了。首先我做了
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
对于程序在启动时启动。如果你不这样做,这个程序就不会启动。
之后,我在解决方案资源管理器窗口中使用了 settings.setting。
这是用于获取设置
chkStartUp.Checked = Properties.Settings.Default.chk;
这是用于更改和保存设置
Properties.Settings.Default.chk = true;
Properties.Settings.Default.Save();
最后,这段代码以管理员身份运行 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);
这就是我解决问题的方法。我希望它可能对其他人有所帮助。如果您有任何其他想法,请与我联系。
推荐阅读
- r - 在每轮比赛后使用R中的dplyr创建具有排名位置的列
- python - python中的分层矩形
- python - 如何从列表列表或元组列表中删除包含某些单词的列表?
- amazon-web-services - 上传到 Amzaon S3 文件夹盯着一个词
- amazon-web-services - 提供的角色没有足够的权限。(服务:AmazonAppConfig)
- python - 如何根据分隔符将一行拆分为多行,并将它们作为键:值对绑定到另一列?
- wordpress - 你能帮我确定目录的位置吗?WordPress 目录位于底部
- javascript - 无法使用 ReactJS 中的映射遍历对象
- graphql - 如何允许石墨烯GraphQl中的任何字段过滤
- laravel-8 - 如果失败则跳到下一个 http 请求(轮询)