/// <summary>
/// 自动启动设置
/// </summary>
/// <param name="started">标记是否自动启动</param>
/// <param name="name">程序名称</param>
/// <param name="path">程序所在路径</param>
public void AutoRunWhenStart(bool started, string name, string path)
{
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey run = HKLM.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\");
if (started)
{
try
{
run.SetValue(name, path);
MessageBox.Show("已设为开机自动启动");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "程序异常");
}
finally
{
run.Close();
HKLM.Close();
}
}
else
{
try
{
run.DeleteValue(name);
MessageBox.Show("已关闭开机自动启动");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "程序异常");
}
finally
{
run.Close();
HKLM.Close();
}
}
}
/// <summary>
/// 检测软件当前是否为开机自动启动状态
/// </summary>
/// <param name="name">注册项名称</param>
/// <returns></returns>
public bool checkIsAutoRun(string name)
{
bool istrue = false;
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey run = HKLM.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\");
if (run.GetValue(name) != null)
{
istrue = true;
}
else
{
istrue = false;
}
run.Close();
HKLM.Close();
return istrue;
}