C# application auto start

Hello,

I have an application which has a form and a check box in it, i will save the check box state in another file so that i know whether it is checked or not... the problem is this .. i want that when the check box is checked the application auto starts when windows starts and vice versa

how can i deal with this maybe by adding a registry key trough the program

Thx


Answer this question

C# application auto start

  • Whoisit

    If you look at the sample you will see that I'm having

    Assembly.GetExecutingAssembly().Location

    as value of the registry value that is written. That is the actual location of the assembly that has been started (the .exe path).



  • Wally9633

    I wrote a class that does that:

    /// <summary>
    /// Enables or disables the autostart (with the OS) of the application.
    /// </summary>
    public static class AutoStarter
    {
     private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";
     private const string VALUE_NAME = "Chili Clipboard";

     /// <summary>
     /// Set the autostart value for the assembly.
     /// </summary>
     public static void SetAutoStart()
     {
      RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
      key.SetValue(VALUE_NAME, Assembly.GetExecutingAssembly().Location);
     }

     /// <summary>
     /// Returns whether auto start is enabled.
     /// </summary>
     public static bool IsAutoStartEnabled
     {
      get
      {
       RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);
       if (key == null)
        return false;

       string value = (string)key.GetValue(VALUE_NAME);
       if (value == null)
        return false;
       return (value == Assembly.GetExecutingAssembly().Location);
      }
     }

     /// <summary>
     /// Unsets the autostart value for the assembly.
     /// </summary>
     public static void UnSetAutoStart()
     {
      RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
      key.DeleteValue(VALUE_NAME);
     }
    }

    You need to replace the VALUE_NAME with the name of your application.



  • David Sadler

    Hi,

    I have a little problem with autostart. I have 2 programs, both are using registry keys to autostart at Windows startup. One of them always starts up, no problem with it. The other one's key almost every time is removed. I don't know whether my antivirus program (NOD32) considers it as some kind of threat but it simply disappears and I don't know its cause. I checked my code to avoid accidental remove of the key but I didn't find anything.
    I use the same code as below in both program:

    Code Snippet

    private void CheckRegistry()
    {
    RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    if (this.checkBoxStartup.Checked)
    {
    if (!checkRegSet(rkApp))
    {
    rkApp.SetValue("App", Application.ExecutablePath.ToString());
    }

    }
    else
    {
    if (checkRegSet(rkApp))
    {
    rkApp.DeleteValue("App");
    }

    }

    }

    private bool checkRegSet(RegistryKey rk)
    {
    if (rk.GetValue("App") == null)
    return false; //not set
    else return true; //set
    }



  • WinFormsUser13232

    Hello thx for your reply

    How will the Operating system trough the registry start the application without having a path for the application

    I mean the value_name will contain the name of the application like .. Application1 for example

    Do i need to specify any location

    thx

  • C# application auto start