none
How to enable and disable start up programs using C# RRS feed

  • Question

  • Hello, I have Windows Forms app. Colleagues I want get list with all start up programs and after disable or enable. I see many examples but nobody cant help me.

            private void SetStartup(string AppName, bool enable)
            {
                string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    
                Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey);
       
                if (enable)
                {
                    if (startupKey.GetValue(AppName) == null)
                    {
                        startupKey.Close();
                        startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                        // Add startup reg key
                        startupKey.SetValue(AppName, Application.ExecutablePath.ToString());
                        startupKey.Close();
                    }
                }
                else
                {
                    // remove startup
                    startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
                    startupKey.DeleteValue(AppName, false);
                    startupKey.Close();
                }
            }

    With this function i want enable or disable start up app but any "appname", program make yourself start up.

    private void ListView4()
            {
                listView4.Items.Clear();
    
                ManagementClass cls = new ManagementClass("Win32_StartupCommand");
                ManagementObjectCollection coll = cls.GetInstances();
                List<string> items = new List<string>();
    
                foreach (ManagementObject obj in coll)
                {
                    String Name = obj["Name"].ToString();
                    String Command = obj["Command"].ToString();
                    String Description = obj["Description"].ToString();
                    String Location = obj["Location"].ToString();
                    String User = obj["User"].ToString();
    
                    ListViewItem newItem = new ListViewItem(Name);
                    newItem.SubItems.Add(Command);
                    newItem.SubItems.Add(Description);
                    newItem.SubItems.Add(Location);
                    newItem.SubItems.Add(User);
                    listView4.Items.Add(newItem);
                }


    This function need give name, command, description and location on start up program, but give me wrong data for someone programs.

    RegistryKey HCU = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                foreach (string Programs in HCU.GetValueNames())
                {
                    MessageBox.Show(Programs);
                }
    
                RegistryKey HLM = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                foreach (string Programs in HLM.GetValueNames())
                {
                    MessageBox.Show(Programs);
                }

    This function return only name on start up programs.

    P.S. Sorry for my english.

    Wednesday, February 26, 2014 10:32 PM

Answers

  • Hi Miro-vt,

    I think the code you provided should be work fine. What you need is to link them into your WinForm app. I make some small changes on your code, you can have a look at it.

    Define a class to store the information from registrykey:
    
    public class StartUpProgram
    
        {
    
            public string Name { get; set; }
    
    
            public string Path { get; set; }
    
            //show name in checkboxitem
    
            public override string ToString()
    
            {
    
                return Name;
    
            }
    
        }
    

    Use the following code to get all start up programs:

    const string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    
                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 });
    
                }
    

    List all items in WinForm control, I choose ListBox to show items:

       foreach (var item in appInfos)
    
                {
    
                    ListBox1.Items.Add(item);
    
                }
    

    When you select a Item in ListBox, you can use the following code to disable start up program.

    StartUpProgram program = listBox1.SelectedItem as StartUpProgram;
    
                    // remove startup
    
                    startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
    
                    startupKey.DeleteValue(program.Name, false);
    
                    startupKey.Close();
    

    Add a Button on your Form, when click it add some code to add start up program.

      startupKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(runKey, true);
    
                // Add startup reg key
    
                startupKey.SetValue(AppName, Application.ExecutablePath.ToString());
    
                startupKey.Close();
    

    Hope useful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 28, 2014 2:16 AM
    Moderator