Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Run the application at Windows startup

Learn how to make your application run at startup using a well placed registry key. Also you'll see how to stop the application from running at startup, and checking its current state.

On Friday, November 18th 2005 at 11:39 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.7 with 74 votes)
Contextual Ads
More C# Resources
Advertisement
The Visual Studio 2005 project below includes a tiny sample application with a checkbox which you can use to definy wether or not the application should run at startup. The project can very easily be converted to older versions of Visual Studio.



Visual Studio 2005 project Download this project (Visual Studio 2005)



Making
the application run or not run at startup involves a few simple lines of code for operating on the Windows Registry. More exactly the registry key we're interested in is Run. The full path to it is HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run or if you want the setting for all the users of the operating system use HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run.



Before we start, make sure you add the following using directive:





using Microsoft.Win32;




Define a RegistryKey object we're going to use for accessing the path to the Run registry key:





// The path to the key where Windows looks for startup applications

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);





You can change CurrentUser to LocalMachine if you want the application to run at startup for all the users of the operating system.



Now here's the code used to make the current application run at startup:





// Add the value in the registry so that the application runs at startup

rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());




Of course, change MyApp to the name of your application as you want it to appear in the System Configuration Utility (msconfig.exe which can be opened by typing msconfig at Start Menu -> Run). Application.ExecutablePath.ToString()retrieves the path to the application executable that's currently running, so we don't have to write it ourselves, not to mention that we don't normally know where the user installs the application.

So basically what we did here is add a value to the Run registry key, containing the name of the application and the path to the executable. That's enough for the executable to run at startup.



Now how do we disable the application from running at startup. Obviously, we delete the registry value we just created:





// Remove the value from the registry so that the application doesn't start

rkApp.DeleteValue("MyApp", false);




And how do we check the current state of the application to see if it's set to run at startup or not.





// Check to see the current state (running at startup or not)

if (rkApp.GetValue("MyApp") == null)

{

   // The value doesn't exist, the application is not set to run at startup

   chkRun.Checked = false;

}

else

{

   // The value exists, the application is set to run at startup

   chkRun.Checked = true;

}




Where chkRun is a Checkbox which gets checked if the application is set to run at startup, and unchecked if it isn't.



There are a few other ways of making your application run at startup, such as adding it to the Startup folder of the Start menu, or adding a line to Win.ini, however using the Run registry key is the most reliable and popular method.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by ChE on Tuesday, February 28th 2006 at 09:35 PM

Thank you :)

by Ela on Thursday, March 16th 2006 at 05:05 AM

Thanks. But... how to do that at the time of installation?

by Andrei Pociu on Thursday, March 16th 2006 at 05:42 AM

You can either create your own installer, or configure the 3rd party installer software to do that. Most setup programs allow you to do that.

by Eric Van de Kerckhov on Sunday, April 30th 2006 at 07:13 PM

Very nice, now I can start up my apps from startup!!

by Loc Le on Thursday, August 10th 2006 at 05:18 AM

Good.
Thank you very much.

by zoran on Tuesday, September 26th 2006 at 09:25 AM

I tried manuially to edit registry, but when I restart comp there is error in app, but i can start it normally and from startup. What can be wrong?
Thanks in advance!

by sumit on Monday, December 17th 2007 at 08:10 AM

code for run on start up!!!!!!1

by student on Tuesday, January 15th 2008 at 07:49 AM

danke schön!!!

by geek_2k on Friday, May 2nd 2008 at 11:51 AM

cool man
now i can run ma app @ system startup


www.hidden-geek.co.cc

by blossoms on Monday, June 30th 2008 at 06:27 AM

Thank you,I will try this and tell you

by maxtic on Sunday, September 14th 2008 at 12:17 PM

thank you very good

by Sakkos panagiotis on Sunday, September 21st 2008 at 03:47 AM


..you just saved my life :) ..very good tutorial ;)..congrats!!

by kris on Friday, October 3rd 2008 at 10:52 AM

This makes the application start on windows startup, but for some reason it "copies" the executable to C:\Windows\System32, is this known?
Btw, i'm using Vista Business edition

by shakil ahmad Haqani on Friday, October 17th 2008 at 09:22 AM

thats great...

by sinner on Friday, January 16th 2009 at 03:58 AM


THAT'S SO COOL THANK , BROTHER .

by Rajat on Wednesday, January 21st 2009 at 09:46 AM

Thanks a lot......was looking around for suck an app.........I was trying to make a winform app run as a service.....this is a good idea

by sason on Wednesday, February 11th 2009 at 03:49 PM

hi ,
if i am not the administrator of the computer is the registry key LocalMachine available for me to set ?
if not will i get an exception ?

by sason on Wednesday, February 11th 2009 at 03:49 PM

hi ,
if i am not the administrator of the computer is the registry key LocalMachine available for me to set ?
if not will i get an exception ?

by Seema Narade on Tuesday, March 24th 2009 at 04:18 AM

Good it helps me
can u tell me how i run exe after every 5 min from start up

by raj on Friday, April 10th 2009 at 08:09 AM

its awsome really helpful..thanks for making this

by rohith on Friday, April 24th 2009 at 05:55 PM

sir... i'm really thankful to you ... it's awesome... very useful to others who are looking for this

by Ashish Tyagi on Saturday, May 16th 2009 at 05:59 AM

Hi Champ,
This is really cool. This is working in Vista also. Thanks*100times. Gr8 Job.

by Diamonddrake on Sunday, May 17th 2009 at 08:36 PM

Thanks for this!

I put this in my windows forms extension library. It works great. Thanks a lot!!!

by Suraj Palapathwela on Wednesday, May 27th 2009 at 08:22 AM

It's very helpful. Thank you!

by Suraj Palapathwela on Wednesday, May 27th 2009 at 08:22 AM

It's very helpful. Thank you!

by sugath on Tuesday, June 9th 2009 at 10:55 AM

Can any body please tell me is this working for Windows Vista

by Tien on Sunday, July 26th 2009 at 09:00 PM

thank you so much

by chk_chk on Thursday, July 30th 2009 at 05:45 AM

Made it simple, Much appreciated.

by work with it in installer on Tuesday, August 4th 2009 at 01:08 AM

hi Thank you very much...
But... how to do that at the time of installation?
I don't know what is 3rd party installer

I want mak einstaller with visual studio .net 2005.
could you tell me how i can it?

by Jimmi on Thursday, October 29th 2009 at 03:07 AM

Nice Article, it works really well on restarting the system. But i am facing one issue, ie the application stops each time when the session state disconnected(accessing via remotedesktop). Can u pls tell me why this is happening?????

by nobody on Monday, November 9th 2009 at 11:41 AM

This does not will work in vista, right?

by BLinK3rO on Sunday, January 24th 2010 at 05:43 AM

Great! thx u very much

by anothen on Sunday, March 14th 2010 at 08:56 AM

Hi! i tried this in windows 7 and it totally wrecked my user account(windows would freeze at welcome screen). so...is this not applicable for 7?

by rev on Wednesday, March 17th 2010 at 06:36 AM

hi,

I cant understand thz tutorial. plz explain me again, i want to start up my website in startup.

regards,
rev

by Kundan Sharma on Thursday, March 25th 2010 at 05:49 AM

Very useful, the way of representation is excellent.

by Kundan Sharma on Thursday, March 25th 2010 at 05:50 AM

Very useful, the way of representation is excellent.

by Kundan Sharma on Thursday, March 25th 2010 at 05:50 AM

Very useful, the way of representation is excellent.

by Kundan Sharma on Thursday, March 25th 2010 at 05:50 AM

Very useful, the way of representation is excellent.

by Kundan Sharma on Thursday, March 25th 2010 at 05:50 AM

Very useful, the way of representation is excellent.

by Kundan Sharma on Thursday, March 25th 2010 at 05:50 AM

Very useful, the way of representation is excellent.

by bikash on Thursday, May 27th 2010 at 04:32 AM

thanks a lot

by Fale Mantim Innocent on Thursday, July 22nd 2010 at 06:35 AM

That was splendid. You saved my arse!

by Fale Mantim Innocent on Thursday, July 22nd 2010 at 06:38 AM

That was splendid. You saved my arse!

by Fale Mantim Innocent on Thursday, July 22nd 2010 at 06:39 AM

That was splendid. You saved my arse!

by javad on Thursday, July 29th 2010 at 02:28 PM

thank u from iran.

by moon@lysa on Wednesday, August 25th 2010 at 10:26 AM

Thank you :)

by dfgd on Monday, September 6th 2010 at 06:23 AM

Thank you

by Wim on Monday, October 18th 2010 at 08:32 AM

Thank u from Belgium.

by Praveen on Wednesday, December 1st 2010 at 06:05 AM

autoRun the application without checkon run at startup

by Praveen on Wednesday, December 1st 2010 at 06:05 AM

autoRun the application without checkon run at startup

by armagan on Tuesday, December 21st 2010 at 03:40 PM

You are one of the people who makes life easier. Thanks.

by bhargav on Tuesday, January 4th 2011 at 02:21 AM

i am try this is in commit method in installer
( RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\LocalMachine\\Run", true);
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());)
but i am getting file not found error when i try to install pls help me out
thanks

by vikas on Friday, January 14th 2011 at 09:42 AM

thank for ur snipet i will try it

and thanks a lot again

by mohamed on Saturday, February 26th 2011 at 04:51 PM

it's so very nice project
thanks a lot
you can visit my site and c# sharp code and java script
http://code2all.blogspot.com/

or visit c sharp part only
http://code2all.blogspot.com/search/label/c sharp

by SomeCoder on Saturday, July 16th 2011 at 03:13 PM

Thank you very much.

This is exactly what I was looking for.

Someone mentioned it does not work on Windows 7.

I use windows 7 and works perfectly for me.

by Kashish Gupta on Saturday, September 10th 2011 at 03:46 PM

Buddy u r great nice code nd nice explanation ...
u r cool dude..great

by Will Graham on Monday, October 10th 2011 at 02:01 PM

For those getting a file not found error, try putting the application name in quotes:

rkApp.SetValue("MyApp", String.Format("\"{0}\"", Application.ExecutablePath));

by free online dating on Sunday, November 20th 2011 at 06:44 AM

Find serial statistics and/or holograms which may usually be used as indicators the fact that sports jerseys will be valid.

by seo company rankings on Monday, December 12th 2011 at 05:44 AM

Put everything in a method, and in the catch block you can recall the method (you can say the method calls itself). Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by on Tuesday, January 17th 2012 at 12:32 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by thirupathi on Thursday, December 6th 2012 at 08:54 AM

if i want to remove this application from the system what i want todo. please helpout. i tested i working goood but, i want to remove this application from my system.

please help me out

by Ahmed on Thursday, March 7th 2013 at 08:34 PM

very helpful topic

by Thusi on Tuesday, November 5th 2013 at 06:41 AM

Very simple and straight forward! Thanx alot man

by Thusi on Tuesday, November 5th 2013 at 06:43 AM

Just started C# 2 days back, that's much helpful. . . . .


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources