Link to home
Start Free TrialLog in
Avatar of srk1982
srk1982

asked on

Adding windows application programatically in windows startup

hi experts,

            I have written an windows application. I will just pass the setup files to my client.
After he installs the setup file, the program will start running. But once the system is shut down or restarted. I want that application to be started automatically without any initiation from the user.

I have done this project in c# and i want to attain this programatically.
ASKER CERTIFIED SOLUTION
Avatar of -DLinder-
-DLinder-
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a more basic solution that doesn't provide the flexibility of the last:
///  DEFINITION OF AUTOSTART FUNCTIONS
 
using Microsoft.Win32;
 
/// <summary>
/// Utility.
/// </summary>
public class Util
{
    private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";
 
    /// <summary>
    /// Sets the autostart value for the assembly.
    /// </summary>
    /// <param name="keyName">Registry Key Name</param>
    /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
    public static void SetAutoStart(string keyName, string assemblyLocation)
    {
        RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
        key.SetValue(keyName, assemblyLocation);
    }
 
    /// <summary>
    /// Returns whether auto start is enabled.
    /// </summary>
    /// <param name="keyName">Registry Key Name</param>
    /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
    public static bool IsAutoStartEnabled(string keyName, string assemblyLocation)
    {
        RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);
        if (key == null)
            return false;
 
        string value = (string)key.GetValue(keyName);
        if (value == null)
            return false;
 
        return (value == assemblyLocation);
    }
 
    /// <summary>
    /// Unsets the autostart value for the assembly.
    /// </summary>
    /// <param name="keyName">Registry Key Name</param>
    public static void UnSetAutoStart(string keyName)
    {
        RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
        key.DeleteValue(keyName);
    }
}
 
///  SAMPLE USAGE IN YOUR APPLICATION
 
string keyName = "MyProgramName";
string assemblyLocation = Assembly.GetExecutingAssembly().Location;  // Or the EXE path.
 
// Set Auto-start.
Util.SetAutoStart(keyName, assemblyLocation);
 
// Unset Auto-start.
if (Util.IsAutoStartEnabled(keyName, assemblyLocation))
    Util.UnSetAutoStart(keyName);

Open in new window

Avatar of HarryNS
HarryNS

RegistryKey hklm =Registry.LocalMachine;
            hklm = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);

            // To add your Application at Windows startup
            hklm.SetValue(Application.ProductName, Application.ExecutablePath);

            // To Remove the startup Application
            hklm.DeleteValue(Application.ProductName);
Other option is to add a shortcut to starup folder