string fAppKeyName="LianLiJZX311";
string fAppPath = "\"" + AppDomain.CurrentDomain.BaseDirectory + "AppMonitor.exe\"";
try
{
RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (rgkRun == null)
{
rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (rgkRun.GetValueNames().FirstOrDefault(p => p == fAppKeyName) == null)
{ //未找到本软件的开机启动注册表信息
rgkRun.SetValue(fAppKeyName, fAppPath);
}
else {
if (rgkRun.GetValue(fAppKeyName).ToString() != fAppPath)
{ //开机启动项的程序路径与当前程序路径不一致
rgkRun.SetValue(fAppKeyName, fAppPath);
}
}
}
catch (Exception ex)
{
MessageBox.Show("无法创建开机启动项!\r\n"+ex.ToString(), "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
}
C# 开机启动
最新推荐文章于 2022-03-12 18:04:28 发布