win10 requireAdministrator设置开机自启动无效的解决方案


开发了一个wpf程序,需要管理员权限,设置了requireAdministrator

同时需要开机自启动,所以添加了注册表:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.SetValue("MyApp", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\""); key.Close(); }
但是在设置成功后,在电脑启动后程序却没有自动启动,找了很多办法,都不行,最后还是在网上找到了解决方案:

https://stackoverflow.com/questions/37440196/c-sharp-application-not-run-on-startup-startup-impact-not-measured-on-windows
就是当我们程序需要管理员权限时,注册表不能使用CurrentUser,需要使用LocalMachine,如下:
using (RegistryKey key = Registry.LocalMachine
.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) 
{
key.SetValue("MyApp", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
key.Close();
}
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM