News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
How to: Make your windows forms application run at Windows startup using C#

Sometimes you need to run your windows forms application when the user log in to windows. Many applications that use this behavior are Windows Live Messenger and Yahoo Messenger. In this article you will learn how to make your application run at startup using C#.

Introduction:

The Windows Registry is a hierarchical database that stores configuration settings and options on Microsoft Windows operating systems. It contains settings for low-level operating system components as well as the applications running on the platform: the kernel, device drivers, services, user interface and third party applications all make use of the Registry. Windows operating system rely on registry to store many applications configurations.

One of the configurations that the windows operating system store in the registry is the applications that should run at windows start up. The registry key that is responsible for storing these configurations is the Run key found under “HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion”.

Using the Code:

To make your application run at startup follow these steps:

1.       Create a new windows application using Visual Studio 2005/2008.

2.       Rename Form1 to StartupForm.

3.       Add a checkbox to the StartupForm and rename it to StartupCheckBox.

4.       Import the Microsoft.Win32 namespace using the following statement:

using Microsoft.Win32;

5.       This namespace contains all the classes used to manage the Registry including adding keys and removing registry keys.

6.       The first class that we use is the Registry Class. This class contains the main registry keys like CurrenyUser.

7.       Implement the StartupCheckBocx CheckChanged event and write the following code:

RegistryKey currentUserRegistry = Registry.CurrentUser;

RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey

    (@"Software\Microsoft\Windows\CurrentVersion\Run", true);

 

if (runRegistryKey != null)

{

    if (StartupToolStripButton.Checked)

        runRegistryKey.SetValue("MyAppName", Application.ExecutablePath);

    else

        runRegistryKey.DeleteValue("MyAppName", false);

}

a.       In the above code we simply create a new instance of the RegistryKey class named currentUserRegistry and set this instance to look at the Current User logical section of the Registry.

b.      We  create a new instance of the RegistryKey class named runRegistryKey that will point to the following registry key path HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

c.       We check that these registry keys already exist in the registry and check if the StartupCheckBox is checked.

d.      If it is checked we add a value to the Run key this value is a key value pain with the key is the name of the application that will be started at startup and the value is the application Executable Path on the hard drive.

e.      If the checkbox is not checked we remove the key from the run and this will disable running the application at Windows Startup.

8.       At this point you can compile and run the application.

9.       Check the StartupCheckBox and close the application.

10.   Logoff windows and log in again.

11.   You will notice that the application will run when you login. But the StartupCheckBox is not checked and you does not know if the application should run at startup or not.

12.   So we need to check the application startup option from the registry. To do so follow these steps:

13.   Implement the StartupForm Load event and write the following code in it this event.

RegistryKey currentUserRegistry = Registry.CurrentUser;

RegistryKey runRegistryKey = currentUserRegistry.OpenSubKey

    (@"Software\Microsoft\Windows\CurrentVersion\Run", true);

 

if (runRegistryKey != null)

{

    if (runRegistryKey.GetValue("MyAppName") == null)

    {

        StartupToolStripButton.Checked = false;

    }

    else

    {

        StartupToolStripButton.Checked = true;

    }

}

a.       In the above code, as in the previous code snippet we create two registry keys one that is mapped to Current User and the second that opens the Run registry key.

b.      We check that these keys exist.

c.       Then we get the startup key of our application using the runRegistryKey.GetValue passing application name key used in the first code snippet.

d.      If we find that the registry key value is null meaning that the application registry key is not found and so the application will not run at startup.

e.      If we found that the registry key value is not null meaning that the application registry key is found and so the application will run at startup.

Note:

                The Application name should be the same for all method calls.

 

14.   Now compile the application and run the application. You will notice that if the application has a key in the run registry key the Startup Checkbox is checked and vice versa.

Now you have an application that can be run at the startup of the windows operating system.

(267) Comments

Blog Message https://www.jigsawconferences.co.uk/venuesearch?search=manchester

There as certainly a great deal to learn about this topic. I love all of the points you made.


Blog Message https://eduniversebylinda.tumblr.com

This is a really good tip particularly to those new to the blogosphere. Brief but very accurate information Thank you for sharing this one. A must read article!


Blog Message https://www.graphicallyspeaking.ca/

It as the little changes which will make the largest changes.


Blog Message http://coronationim.com/

Merely a smiling visitant here to share the love (:, btw outstanding design. Individuals may form communities, but it is institutions alone that can create a nation. by Benjamin Disraeli.


Blog Message http://jules1873pl.innoarticles.com/start-off-by-deciding-many-knick-knacks-can-make-your-room-feel-cluttered-and-disorganized-which-can-actually-cause-sleep-trouble

LOUIS VUITTON WALLET ??????30????????????????5??????????????? | ????????


Blog Message http://betterimagepropertyservices.ca/

Major thankies for the blog article. Awesome.


Blog Message http://proline.physics.iisc.ernet.in/wiki/index.php/..._Info_Num_41_From_905

Really good article! Also visit my blog about Clomid challenge test


Blog Message https://www.liveinternet.ru/users/henry_cotton/blog#post434537648

Really appreciate you sharing this article post.Much thanks again. Will read on...


Blog Message http://betterimagepropertyservices.ca/

Pretty! This has been an extremely wonderful article. Thanks for supplying this information.


Blog Message https://topbestbrand.com/%E0%B8%AD%E0%B8%A2%E0%B8%B2%E0%B8%81%E0%B8%82%E0%B8%B2%E0%B8%A7-%E0%B8%97%E0%B8%B3%E0%B9%84%E0%B8%87%E0%B8%94%E0%B8%B5-10-%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%8A%E0%B9%88%E0%B8%A7%E0%B8%A2/

whoah this weblog is wonderful i like reading your articles. Keep up the good paintings! You already know, many people are looking around for this information, you can help them greatly.


Blog Message http://chaseresults.com/mail_to_friend/show/?url=http://ecoel.org/story.php%3Fid=1018

Some truly good content about this web website, appreciate it for info. A conservative can be a man which sits and also thinks, mostly sits. by Woodrow Wilson.


Blog Message https://www.coindesk.com/bitcoin-lightning-payments-slowly-becoming-less-reckless/

It as hard to find educated people in this particular topic, but you seem like you know what you are talking about! Thanks


Blog Message https://usethebitcoin.com/fundstrats-tom-lee-says-that-bitcoin-could-skyrocket-more-than-70-in-the-next-week/

Say, you got a nice blog article. Will read on


Blog Message https://www.wrpsa.com/official-history/

You made some clear points there. I looked on the internet for the issue and found most individuals will go along with with your site.


Blog Message http://www.seoinvancouver.com/index.php/78-seo-vancouver/577-seo-surrey-bc

Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Actually Excellent. I am also an expert in this topic so I can understand your effort.

First Previous 1 2 3 4 5 6 7 8 9 10  ... Next Last 
Rating
Name
Email
Comment