///
/// 将本程序设为开启自启
///
/// 自启开关
///
private bool SetMeStart(bool onOff)
{
return SelfRunning(onOff, appName, appPath);
}
///
/// 判断注册键值对是否存在,即是否处于开机启动状态
///
/// 键值名
///
private static bool IsExistKey(string keyName)
{
try
{
RegistryKey local = Registry.LocalMachine;
RegistryKey runs = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
string[] runsName = runs.GetValueNames();
foreach (string strName in runsName)
{
if (strName.ToUpper() == keyName.ToUpper())
{
return true;
}
}
return false;
}
catch
{
return false;
}
}
///
/// 写入或删除注册表键值对,即设为开机启动或开机不启动
///
/// 是否开机启动
/// 应用程序名
/// 应用程序路径带程序名
///
private static bool SelfRunning(bool isStart, string exeName, string path)
{
try
{
RegistryKey local = Registry.LocalMachine;
RegistryKey key = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (key == null)
{
local.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
}
//若开机自启动则添加键值对
if (isStart)
{
key.SetValue(exeName, path);
}
else//否则删除键值对
{
key.DeleteValue(exeName);
}
key.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
return true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
HideMainForm();
e.Cancel = true;
}
厉害,开机自启,我也在学.NET