Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Add application under startup through programme

Tags:

c#

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

like image 792
user209293 Avatar asked Jul 14 '10 12:07

user209293


Video Answer


1 Answers

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.

like image 61
Manjoor Avatar answered Sep 28 '22 18:09

Manjoor