65.9K
CodeProject is changing. Read more.
Home

How to execute program time to time (reading and writing values in system registry)

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.25/5 (18 votes)

Jun 11, 2007

CPOL
viewsIcon

21321

downloadIcon

221

this code will help you to read an exisiting value or to create a value in system registry and execute program time to time

Download Sample.zip - 521.5 KB

Introduction

When we are creating a program some of us in order to save settings of application, we inteded to do is to have file processing techiniques, like .ini /.dat Files or an xml files in order to check the recent setting that have done. So we can do it by just simply manipulating the system registry files...

Background

The basic idea here is how to store settings on your system regitry for your application:

  • Read and write setting in system regstry of your computer.
  • Cheking the recent state of your application
  • Set your application at windows strartup.

Using the code

Here is the sample code that i made using C# language (Dotnet Framework ver.2.0 or higher).

This sample code is use to execute application at windows start up and will show in every 30 minutes of an hour.

<i made this stuff actually for a naughty things.. hmm.. let me say to scary some body by just viewing a scarry picture on their computer unexepectedly> =} ENJOY!! HERE It IS.

//
// 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {    
            this.Hide();
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Minute.ToString() == "30")
            {
                this.Show();
            }
            else
            {
                this.Hide();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                timer1_Tick(sender, e);
                bool isKeyExist = false;
                string strSubkey = @"Software\Microsoft\Windows\CurrentVersion\Run";
                RegistryKey Mykey = Registry.CurrentUser.OpenSubKey(strSubkey);
                string[] strSearcKey = Mykey.GetValueNames();
                Mykey.Close();
                foreach (string strSearch in strSearcKey)
                {
                    if (strSearch.ToUpper().Equals("ERIC"))
                    {
                    isKeyExist = true;
                    break;
                    }
                }
                if (isKeyExist == false)
                {
                    Mykey = Registry.CurrentUser.CreateSubKey(strSubkey);
                    Mykey.SetValue("ERIC", Application.ExecutablePath, RegistryValueKind.String);
                    Mykey.Close();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
    }
}
//