代码如下:
//路径, 添加开机启动/删除开机启动
public static void SetAutoRun(string fileName, bool isAutoRun)
{
RegistryKey reg = null;
try
{
if (!System.IO.File.Exists(fileName))
throw new Exception("该文件不存在!");
String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (reg == null)
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (isAutoRun)
reg.SetValue(name, fileName);
else
reg.SetValue(name, false);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
if (reg != null)
reg.Close();
}
}
您可能感兴趣的文章:C#实现程序开机启动的方法C#实现将应用程序设置为开机启动的方法C#实现开机自动启动设置代码分享C#设置开机启动项、取消开机启动项C#代码设置开机启动示例C#如何防止程序多次运行的技巧C#编程中设置程序只可被运行一次的方法在Linux上运行C#的方法C#操作注册表的方法详解c#读写注册表示例分享C# 注册表 操作实现代码C#操作注册表的方法C#设置软件开机自动运行的方法(修改注册表)