Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello guys, I need a little help. So, I have made an App in Windows Forms, it works fine and now I want to start this App everytime when Windows Starts. I have successfully made it with this code:
RegistryKey add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
add.SetValue("Battery", "\"" + Application.ExecutablePath.ToString() + "\"");


But I have edited one Checkbox on Form. If it's checked, it has to start when Windows Loads.If it's not checked, it haven't to start together with Windows. So I have made an Event CheckedChanged which detects when checkbox is changed but I donn't know how to save value of Checkbox so that after shutdown it wouldn' be deleted.

What I have tried:

I have found in Internet that I can do it with "Serializable". I have only understood, that I have to write it before class and than save information in "XML, Binarystream, Memory Stream". But I donn't know what have I to save and how ? Can someone Help me ?
Posted
Updated 13-Aug-17 20:02pm
Comments
BillWoodruff 14-Aug-17 2:08am    
Do you need to consider security issues here ? Depending on context and Win configuration, writing to the Registry may be restricted.

Another, simpler, technique is to put a ShortCut (.lnk file) to your app in the Windows 'StartUp Folder. Ask, if you wish to see an example of this.

This Google Search: c# run app on windows startup[^] turned up this: c# - How do I set a program to launch at startup - Stack Overflow[^]:

C#
using Microsoft.Win32;

private void SetStartup()
{
	RegistryKey rk = Registry.CurrentUser.OpenSubKey
		("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

	if (chkStartUp.Checked)
		rk.SetValue(AppName, Application.ExecutablePath.ToString());
	else
		rk.DeleteValue(AppName,false);            

}
 
Share this answer
 
Another way to achieve this is to create an Application Setting [^] of Type Bool. In this example, the FormClosing EventHandler compares the CheckBox state to the Setting, and then adds, or deletes, the Registry Key if necessary.

Set the 'Scope parameter for your 'bool Setting to 'User, not 'Application.
using System;
using System.Windows.Forms;

namespace AutoOpen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool isAutoStartNow, isChecked;

        private void Form1_Load(object sender, EventArgs e)
        {
            isAutoStartNow = (bool)Properties.Settings.Default["AutoStart"];

            checkBox1.Checked = isAutoStartNow;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isChecked = checkBox1.Checked;

            isAutoStartNow = (bool)Properties.Settings.Default["AutoStart"];

            // no change necessary
            if (isAutoStartNow == isChecked) return;

            Properties.Settings.Default["AutoStart"] = isChecked;

            if (isChecked)
            {
                // set the registry key
            }
            else
            {
                // delete thr registry key
            }

           // save setting
            Properties.Settings.Default.Save();
        }
    }
}
 
Share this answer
 
v2
Comments
dave_bulac 14-Aug-17 13:33pm    
Thanks a lot.
dave_bulac 14-Aug-17 13:35pm    
But there, where I have edited Settings is one Column Named Value, and it's now setted to false for my bool. What does it mean ?
BillWoodruff 14-Aug-17 14:52pm    
When you create a setting, the setting will be set to the default value for the Type of the setting. For a Bool, that value is 'false. But, you can edit the value, change it to 'true, if you wish in the settings designer.
Suppose, if you attempt to open the key you're adding when your program loads, if it's there means you must start checked, if user unchecks, remove the key. If user then checks it again, try to open key, if it's not there, create.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900