Take the 2-minute tour ×

I am working on a c# application in which i want to load the application as windows start up or as user log in. I am working on Windows & with VS2010. I am writing following code to use registry key but it gives me error of access denied.

    RegistrySecurity RS = new RegistrySecurity();
    RS.AddAccessRule(new RegistryAccessRule(Environment.UserName.ToString(),RegistryRights.WriteKey | RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
    RegistryKey reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVer­sion\\Run",RegistryKeyPermissionCheck.ReadWriteSubTree,RS);
    reg.SetValue("Pratham", Application.ExecutablePath.ToString());

I don't have any idea about running app in side tray please help me with it. Thank you in advance.

share|improve this question
Please explain what you mean with side tray. –  CrashproofCode Jun 5 at 19:45
@CrashproofCode with side tray i mean the system tray which is there on the left side of clock on task bar. –  user3289174 Jun 5 at 19:47
Take a look at System.Windows.Forms.NotifyIcon. –  James R. Jun 5 at 19:49
@JamesR. is it for system tray? –  user3289174 Jun 5 at 19:50

1 Answer 1

You can use the following code:

String programName = "Test Name";
String programPath = "C:\\test.exe";

Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run", programName, programPath);

The reason you get access denied though is that you might need to run the program as administrator.

To add your program to the "side tray" add a NotifyIcon to your form from the toolbox or simply use:

NotifyIcon icon = new NotifyIcon();
share|improve this answer
but is the current user key fine? i mean to say that it should work for all user. how to write to registries using program coding because the app will be installed on different user machines. –  user3289174 Jun 5 at 19:48
Sorry, I changed it now. It should be HKEY_LOCAL_MACHINE. –  CrashproofCode Jun 5 at 19:51
how to run program as administrator when debugging it? and also when every user installs it on their machine and it needs to be started as their system start up how will be implemented? –  user3289174 Jun 7 at 15:25

Your Answer


