[WPF]程序随系统自启动

版权声明:本文为本人原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37316917/article/details/86686657

代码



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;

否则会报错

猜你喜欢

转载自blog.csdn.net/m0_37316917/article/details/86686657