Loading

[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;

否则会报错

posted @ 2019-01-29 10:47  李正浩  阅读(221)  评论(0编辑  收藏  举报