Friday, November 12, 2010

Tip: Set/Remove program to run at System Startup

In this post, I'm providing helper methods to set/remove your non-service programs be automatically launched when Windows boots.

How to use:
To set your program to run every time the systems startup
ChoApplication.RunAtSystemStartup();
To set your application to run once during the systems startup
ChoApplication.RunOnceAtSystemStartup();

To remove your program from running everytime systems startup
ChoApplication.RunAtSystemStartup(true);
To remove your application from running once during the systems startup
ChoApplication.RunOnceAtSystemStartup(true);

Below is the implementation of the helper methods that we talked about. Feel free to use them. As usual, please drop your comments.

public static class ChoApplication
{
   private const string RegRunSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
   private const string RegRunOnceSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce";

   /// <summary>
   /// Registry Key point to SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run
   /// </summary>
   private static readonly RegistryKey _rkAppRun;

   /// <summary>
   /// Registry Key point to SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce
   /// </summary>
   private static readonly RegistryKey _rkAppRunOnce;

   static ChoApplication()
   {
       _rkAppRun = Registry.CurrentUser.OpenSubKey(RegRunSubKey, true);
       if (_rkAppRun == null)
          _rkAppRun = Registry.CurrentUser.CreateSubKey(RegRunSubKey);

       _rkAppRunOnce = Registry.CurrentUser.OpenSubKey(RegRunOnceSubKey, true);
       if (_rkAppRunOnce == null)
          _rkAppRunOnce = Registry.CurrentUser.CreateSubKey(RegRunOnceSubKey);
   }

   #region RunAtSystemStartup Overloads

   /// <summary>
   /// Set/Remove the running executable to run at system startup
   /// </summary>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunAtSystemStartup()
   {
       return RunAtSystemStartup(false);
   }

   /// <summary>
   /// Set/Remove the running executable to run at system startup
   /// </summary>
   /// <param name="remove">true, it remove the application from running at system startup.</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunAtSystemStartup(bool remove)
   {
       string appLocation = Assembly.GetEntryAssembly().Location;
       return RunAtSystemStartup(Path.GetFileNameWithoutExtension(appLocation), appLocation);
   }

   /// <summary>
   /// Set/Remove the running executable to run at system startup
   /// </summary>
   /// <param name="appName">A name to represent the application name in the registry</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunAtSystemStartup(string appName)
   {
       return RunAtSystemStartup(appName, false);
   }

   /// <summary>
   /// Set/Remove the running executable to run at system startup
   /// </summary>
   /// <param name="appName">A name to represent the application name in the registry</param>
   /// <param name="remove">true, it remove the application from running at system startup.</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunAtSystemStartup(string appName, bool remove)
   {
       if (String.IsNullOrEmpty(appName))
          throw new ArgumentException("AppName is missing.");

       return SetValueToRegistry(_rkAppRun, appName, Assembly.GetEntryAssembly().Location, remove);
   }

   /// <summary>
   /// Set/Remove the running executable to run at system startup
   /// </summary>
   /// <param name="appName">A name to represent the application name in the registry</param>
   /// <param name="appLocation">Full path of the application to be set.</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunAtSystemStartup(string appName, string appLocation)
   {
       return SetValueToRegistry(_rkAppRun, appName, appLocation, false);
   }

   #endregion RunAtSystemStartup Overloads

   #region RunOnceAtSystemStartup Overloads

   /// <summary>
   /// Set/Remove the running executable to run once at system startup
   /// </summary>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunOnceAtSystemStartup()
   {
       return RunOnceAtSystemStartup(false);
   }

   /// <summary>
   /// Set/Remove the running executable to run once at system startup
   /// </summary>
   /// <param name="remove">true, it remove the application from running at system startup.</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunOnceAtSystemStartup(bool remove)
   {
       string appLocation = Assembly.GetEntryAssembly().Location;
       return RunOnceAtSystemStartup(Path.GetFileNameWithoutExtension(appLocation), appLocation);
   }

   /// <summary>
   /// Set/Remove the running executable to run once at system startup
   /// </summary>
   /// <param name="appName">A name to represent the application name in the registry</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunOnceAtSystemStartup(string appName)
   {
       return RunOnceAtSystemStartup(appName, false);
   }

   /// <summary>
   /// Set/Remove the running executable to run once at system startup
   /// </summary>
   /// <param name="appName">A name to represent the application name in the registry</param>
   /// <param name="remove">true, it remove the application from running at system startup.</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunOnceAtSystemStartup(string appName, bool remove)
   {
       if (String.IsNullOrEmpty(appName))
          throw new ArgumentException("AppName is missing.");

       return SetValueToRegistry(_rkAppRunOnce, String.Format("!{0}", appName), Assembly.GetEntryAssembly().Location, remove);
   }

   /// <summary>
   /// Set/Remove the running executable to run once at system startup
   /// </summary>
   /// <param name="appName">A name to represent the application name in the registry</param>
   /// <param name="appLocation">Full path of the application to be set.</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   public static bool RunOnceAtSystemStartup(string appName, string appLocation)
   {
       return SetValueToRegistry(_rkAppRunOnce, String.Format("!{0}", appName), appLocation, false);
   }

   #endregion RunOnceAtSystemStartup Overloads

   #region Shared Methods (Private)

   /// <summary>
   /// Set/Remove the running executable to run at system startup
   /// </summary>
   /// <param name="appName">A name to represent the application name in the registry</param>
   /// <param name="appLocation">Full path of the application to be set.</param>
   /// <returns>true, if the operation is successfull. Otherwise, false.</returns>
   private static bool SetValueToRegistry(RegistryKey regKey, string appName, string appLocation, bool remove)
   {
       if (String.IsNullOrEmpty(appName))
          throw new ArgumentException("AppName is missing.");

       if (!remove && String.IsNullOrEmpty(appLocation))
          throw new ArgumentException("AppLocation is missing.");

       if (regKey == null)
          return false;

       if (remove)
          regKey.DeleteValue(appName);
       else
          regKey.SetValue(appName, appLocation);

       return true;
   }

   #endregion Shared Methods (Private)
}

No comments:

Post a Comment