Monday, November 28, 2016

Create Startup Program using C#

 This post shows how to launch your application on system startup using few lines of codes in C#.Launching your application at windows start up is useful for checking updates etc.Create a new windows form application and design a form as shown below.


Source Code

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 RunProgramOnStartup
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
  RegistryKey myKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                myKey.SetValue("DemoProgram", Application.ExecutablePath.ToString());
                myKey.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in adding Key value...");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                RegistryKey myKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                myKey.DeleteValue("DemoProgram", false);
                myKey.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }
    }
}


Download Source Code

Source Code








No comments:

Post a Comment