question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked MiPakTeh-6272 commented

Remove program startUp using Registry

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.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered MiPakTeh-6272 commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank RLWA32.Follow your step but error at code Line;
startupKey.DeleteValue(program.Name, false);

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=ListReg_
StackTrace:
at ListReg_.Form1.button4_Click(Object sender, EventArgs e) in C:\Users\family\source\repos\ListReg_\ListReg_\Form1.cs:line 77

Thank.

0 Votes 0 ·

It didn't throw that exception when I tested your code on my system to delete an entry that I placed into the registry. By the way, the code fills the listbox4 control but the button click gets the selection from listbox1. Maybe that's the problem you see ( I fixed it for my test). And of course the code will throw the exception if nothing is actually selected in the relevant listbox when the button is clicked.

0 Votes 0 ·

Thank you very very much .I miss that .It's listBox4.
sorry.
Working.

0 Votes 0 ·