C# 将程序添加到启动项 (写入注册表),及从启动项中删除
2012-06-07 15:00
465 查看
#region 将程序添加到启动项
/// <summary>
/// 注册表操作,将程序添加到启动项
/// </summary>
public static void SetRegistryApp()
{
try
{
Microsoft.Win32.RegistryKey Reg;
string ShortFileName = Application.ProductName;
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Reg == null)
{
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
Reg.SetValue(ShortFileName, Application.ExecutablePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 将程序从启动项中删除
/// <summary>
/// 注册表操作,删除注册表中启动项
/// </summary>
public static bool DeleteRegisterApp()
{
string ShortFileName = Application.ProductName; //获得应用程序名
try
{
Microsoft.Win32.RegistryKey Reg;
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Reg == null)
{
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
Reg.DeleteValue(ShortFileName, false);
}
catch (Exception ex)
{
return false;
}
return true;
}
#endregion
/// <summary>
/// 检查当前程序是否在启动项中
/// </summary>
/// <returns></returns>
public static bool CheckExistRegisterApp()
{
string ShortFileName = Application.ProductName; //获得应用程序名
bool bResult = false;
try
{
Microsoft.Win32.RegistryKey Reg;
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Reg == null)
{
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
foreach (string s in Reg.GetValueNames())
{
if (s.Equals(ShortFileName))
{
bResult = true;
break;
}
}
}
catch (Exception ex)
{
return false;
}
return bResult;
}
/// <summary>
/// 注册表操作,将程序添加到启动项
/// </summary>
public static void SetRegistryApp()
{
try
{
Microsoft.Win32.RegistryKey Reg;
string ShortFileName = Application.ProductName;
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Reg == null)
{
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
Reg.SetValue(ShortFileName, Application.ExecutablePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region 将程序从启动项中删除
/// <summary>
/// 注册表操作,删除注册表中启动项
/// </summary>
public static bool DeleteRegisterApp()
{
string ShortFileName = Application.ProductName; //获得应用程序名
try
{
Microsoft.Win32.RegistryKey Reg;
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Reg == null)
{
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
Reg.DeleteValue(ShortFileName, false);
}
catch (Exception ex)
{
return false;
}
return true;
}
#endregion
/// <summary>
/// 检查当前程序是否在启动项中
/// </summary>
/// <returns></returns>
public static bool CheckExistRegisterApp()
{
string ShortFileName = Application.ProductName; //获得应用程序名
bool bResult = false;
try
{
Microsoft.Win32.RegistryKey Reg;
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Reg == null)
{
Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
}
foreach (string s in Reg.GetValueNames())
{
if (s.Equals(ShortFileName))
{
bResult = true;
break;
}
}
}
catch (Exception ex)
{
return false;
}
return bResult;
}
相关文章推荐
- C# 将程序添加到启动项 (写入注册表),及从启动项中删除
- 关于C# 读取 写入 删除 注册表简单例子
- windows添加或删除程序中的程序在注册表中的位置
- c# 实现 通过写入注册表 记住 winform程序 登录名
- 注册表写入读取和启动项删除
- 操作注册表添加/删除系统的启动项
- 用注册表添加,删除开机启动程序
- C#操作注册表实现添加/删除可信站点
- C#添加删除防火墙例外(程序、端口)
- C# 程序到期将时间写入注册表代码
- C#把自写的卸载程序添加到控制面板的添加删除程序目录中
- C# 程序到期将时间写入注册表代码
- windows一段添加、删除注册表的bat代码,用于系统启动时打开某个程序
- c# 代码 添加或删除程序开机启动
- MFC利用注册表添加开机启动项和删除开机启动项
- C#使用注册表添加删除开机启动项
- c# 代码 添加或删除程序开机启动
- [转]C#(VB.NET)操作Windows自带的防火墙 之 添加/删除允许通过防火墙的例外程序
- 正常卸载不了,可通过删除注册表的方法,去年添加删除程序中的记录
- C#把自写的卸载程序添加到控制面板的添加删除程序目录中