Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET

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

Rate me:
Please Sign up or sign in to vote.
1.25/5 (20 votes)
26 Jul 2007CPOL 21.1K   221   14   2
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.

C#
//
// 
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); }
        }
    }
}
//

 

License

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


Written By
Other
Philippines Philippines
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAlternative Pin
JaseNet26-Jun-07 6:54
JaseNet26-Jun-07 6:54 
GeneralRe: Alternative Pin
FredericSivignonPro17-Jul-07 5:17
FredericSivignonPro17-Jul-07 5:17 
Rights.

The .NET Framework provides (and more Smile | :) ) every things to manage application settings.

Moreover, Microsoft discourage the usage of the registry to store application settings with .NET applications.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.