[WPF]程序随系统自启动

代码



Microsoft.Win32.RegistryKey key =                  

Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run",(bool)btn.IsChecked);

Assembly curAssembly = Assembly.GetExecutingAssembly();

key.SetValue(curAssembly.GetName().Name, curAssembly.Location);

 存在问题

 如果调成了随系统自启动的话,并且中间使用过Environment.CurrentDirectory,得到的不是程序目录,而是C:/Windows/System32的目录,因此需要改成使用Application.StartupPath,注意命名空间是

using System.Windows.Forms;

否则会报错

https://github.com/li-zheng-hao
原文地址:https://www.cnblogs.com/lizhenghao126/p/11053589.html