Remove program startUp using Registry

MiPakTeh 1,476 Reputation points
2021-09-25T13:19:46.057+00:00

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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,616 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 43,046 Reputation points
    2021-09-25T13:57:54.417+00:00

    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">


0 additional answers

Sort by: Most helpful