This blog has moved to http://ikhwanhayat.net

Monday, July 18, 2005

Making Your Application Run On Startup

There are several ways to make your application run automatically on Windows startup. The easiest one is to put a shorcut to it in Startup folder in the Start menu. Another not too simple way is to do it through the registry. Run the "regedit" and manoeuvre your way to this key "My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run". As you can see, the values inside this key are applications that will be run on Windows startup (actually, after you log in). To include your app into the list, you just have to easily add a new "String Value" here with any name as the "Value name" and the path to your app as the "Value data". That's it.. Another way to view this list is from the "msconfig", at the "Startup" tab. But from here you can only enable and disable the item. So to add a new one you still have to go through the registry as I've mention above. And now for the geekier part (as if editing the registry is not geek enough). If you're an application maker, sometimes you want the user to be able to select whether the application will run at startup by checking a checkbox or something. So this is just a matter of manipulating the registry from your code. In .NET programming, it only requires some few simple lines.

using System.Diagnostics;
using Microsoft.Win32;

string appPath = Process.GetCurrentProcess().MainModule.FileName;
string strKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(strKey, true);

// to add into startup list
regKey.SetValue("My Cute Application", appPath);

// to remove from startup list
regKey.DeleteValue("My Cute Application");
And you're done... In fact we can put all of those in just one line. But I won't do it here because it will be long and putting it in a <pre> will mess up my site layout.

0 Comments:

Post a Comment

<< Home