Write access to HKEY_LOCAL_MACHINE (Microsoft.Win32.Registry.LocalMachine) requires Administrator privileges. So you can add a manifest to your application that specifies <requestedexecutionlevel level="requireAdministrator">
Remove program startUp using Registry
MiPakTeh
1,476
Reputation points
Hi All,
What I try to Do;
Read registry by LocalMachine then put in ListBox.By click Button_4 we can remove that program from startUp.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ListReg_
{
public partial class Form1 : Form
{
public class StartUpProgram
{
public string Name { get; set; }
public string Path { get; set; }
//show name in checkboxitem
public override string ToString()
{
return Name;
}
}
const string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
public Form1()
{
InitializeComponent();
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
using (RegistryKey startupKey = Registry.LocalMachine.OpenSubKey(runKey))
{
var valueNames = startupKey.GetValueNames();
var appInfos = valueNames
.Where(valueName => startupKey.GetValueKind(valueName) == RegistryValueKind.String)
.ToDictionary(valueName => valueName, valueName => startupKey.GetValue(valueName).ToString())
.Select(s => new StartUpProgram { Name = s.Key, Path = s.Value });
foreach (var item in appInfos)
{
listBox4.Items.Add(item);
}
}
}
private void button4_Click(object sender, EventArgs e)
{
StartUpProgram program = listBox1.SelectedItem as StartUpProgram;
// remove startup
RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
startupKey.DeleteValue(program.Name, false);
startupKey.Close();
}
}
}
Thank.