user209293
user209293

Reputation: 889

c# Add application under startup through programme

Want to add/delete an Application under Windows-> All Programs -> StartUp through c# program.

Would appreciate the code / direction to do the above.

Regards Raju

Upvotes: 5

Views: 2646

Answers (1)

Manjoor
Manjoor

Reputation: 4199

you can run it every time windows start by using just 2 line of code below

    RegistryKey Key =  Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);                       
    Key.SetValue("AppName", System.Reflection.Assembly.GetEntryAssembly().Location);

If you realy need to create a startup shortcut, here is the code

  private void CreateShortcutInStartUP()
        {
            try
            {
                Assembly code = Assembly.GetExecutingAssembly();
                String company =  Application.CompanyName;
                String ApplicationName = Application.ProductName;

                if( company != "" && ApplicationName != "") 
                {
                    String DesktopPath= Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + ApplicationName + @".appref-ms";
                    String ShortcutName= Environment.GetFolderPath(Environment.SpecialFolder.Programs) + @"\" + company + @"\" + ApplicationName + @".appref-ms";
                    if (System.IO.File.Exists(ShortcutName))
                        System.IO.File.Copy(ShortcutName, DesktopPath, true);

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

I am currently using above code so you can just copy paste. Make sure you have setup company name.

Upvotes: 8

Related Questions