/// <summary>
/// 开机自启动
/// </summary>
/// <param name="fileName">文件路径</param>
/// <param name="isAutoRun">是否自启动</param>
public void SetAutoRun(string fileName, bool isAutoRun)
{
RegistryKey reg = null;
try
{
if (!System.IO.File.Exists(fileName))
{
ShowInformation("开机自启动未找到应用程序位置\n", Color.Red);
}
String name = "PowerControl";
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (reg == null)
{
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
//reg = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
}
if (isAutoRun)
{
reg.SetValue(name, fileName);
}
else
{
reg.SetValue(name, false);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
ShowInformation("注册表写入开机自启动异常\n", Color.Red);
}
finally
{
if (reg != null)
reg.Close();
}
}
C#操作开机自启动(写进注册表)
最新推荐文章于 2022-08-23 11:32:07 发布