Launch an Application at Windows Startup

C#.Net

Introduction
There are a number of ways by which a program can be launched automatically at Windows startup. We can write the path and name of the program to autoexec.bat or use Win32 API functions to write to the Load= or Run= lines of Win.ini. But the better methods to do the same are to use the registry or the system's Startup folder.
The Startup folder method is easy to use as we just need to place a shorcut of the application we want to run, in the system's Startup folder. To prevent that application from running at startup we can simply remove its shortcut from the system's Startup folder.
To use the registry we need to write the startup information in a specific registry key. We can write a value entry to any of the following registry keys as per requirement:-
  1. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
    Used to launch a program when a particular user logs in.
  2. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnceUsed to launch a program the next time when a particular user logs in and then removes its value entry from the registry.

  3. HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunUsed to launch a program at system startup irrespective of the user who logs in.

  4. HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnceUsed to launch a program the next time when system starts and then removes its value entry from the registry.
I have used the above 4 registry keys to add/remove a program from system's Startup and developed a small library named "StartupApplication" to do so.

Using the Library "StartupApplication"Below shown diagram is the class diagram for "SystemStartup" class defined in "StartupApplication" namespace.

In above class diagram you will see that "SystemStartup" class contains two nested classes "CurrentUser" and "AllUsers". Both these classes have two static methods "AddApplication" and "RemoveApplication".
To add a program to system's startup for a particular user we can use the following piece of code:-
SystemStartup.CurrentUser.AddApplication(
              "Name of the program",
              "Location of the program's exe file",
              SystemStartup.RunApplication.AlwaysAfterStartup
                                        );

To add a program to system's startup for all users just replcae "CurrentUser" with "AllUsers" in above code:-
SystemStartup.AllUsers.AddApplication(
              "Name of the program",
              "Location of the program's exe file",
              SystemStartup.RunApplication.AlwaysAfterStartup
                                      );

To remove a program from system's startup for a particular user we can use the following piece of code:-
SystemStartup.CurrentUser.RemoveApplication("Program Name");

To remove a program from system's startup for all users just replcae "CurrentUser" with "AllUsers" in above code:-
SystemStartup.AllUsers.RemoveApplication("Program Name");

One enumeration named "RunApplication" is also their in "System Startup" class as shown in its class diagram. This is used to specify Run or RunOnce feature of startup program.


13 comments:

Asim said...

hellow! i am trying to switch led's from parallel port using c# but when i run program i am facing this error saying this [SEHException was unhandeled] and External component has thrown an exception that is error how to debug it plaese help me.

Anonymous said...

Hi Jaspreet,

I would like to meet you regarding a project.

My contact number is 09910353378.

Thanks
DJ

Anonymous said...

Please contact me at lacayo1966@hotmail.com regarding a project. Thank.

Anonymous said...

Thanks, very good application ...

kanchana said...

There are lots of information about latest technology and how to get trained in them, like this have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies. By the way you are running a great blog. Thanks for sharing this.
SAS training in chennai

Unknown said...

the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating.
Informatica Training in Chennai

Sowmiya said...

your c language related concepts thus it is really well done to a great job thus it is nice too and i got more information from your blog. i like this article and it is very well good too to practice the concepts.


Digital Marketing Company in Chennai

Unknown said...

I cant wait to check out some of these blogs! I’ve really wanted to start learning more about cars and auto repairs lately and I think this will help a lot. I think it can save my family some money if we knew how to do some repairs at home.! Thanks again for all the options.
seo company in chennai

Shalini said...

Very nice post here thanks for it .I always like and such a super contents of these
post.Excellent and very cool idea and great content of different kinds of the valuable
information's. seo company in chennai

Unknown said...

Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information.Any information are commands like to share him.

digital marketing course in chennai

Padhma said...

Thanks for appreciating. Really means and inspires a lot to hear from you guys.I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..Believe me, This is very helpful for me.

Car Wash Services in Mumbai

Akshaysri said...

Such a great articles in my carrier, It's wonderful commands like easiest understand words of knowledge in information's.
GMAT coaching chennai

Unknown said...

Thanks admin for sharing the unique content, you have done a great job I appreciate your effort and I hope you will get more positive comments from the web users.
PHP Training in Chennai