Monday, January 28, 2008

Add windows form application in registry to run at startup

If you want to install a windows application on the client machine, specially if you have to deploy a smart client application and you also want to run that application on startup, then you can add the path of the executable of the application in the registry. In this article, I'll show how you can do this.

First of all, import following namespace in your C# Windows Form application project.

using Microsoft.Win32;

Now, you have to add following code on the application load.

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

Above line of code opens a registry sub key which allows to run the application at startup. In the following line you have to check that if your required registry key value is not there then add it otherwise you can ignore, so you wouldn't add it every time your application loads.

if (rkApp.GetValue("MyAppRegKey") == null)
{
rkApp.SetValue("MyAppRegKey", Application.ExecutablePath.ToString());
}

After running the application for the first time you'll see MyAppRegKey at the following path in registry editor SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run. You'll have to look for this path in the current user category of the registry editor

Oh...by the way, you can open registry editor as under:

Start -> Run -> Type regedit and Press Enter key.

No comments: