1

I want to run a C# application at startup. I used this code, which I found here:

private void SetStartup(bool enable)
    {
        string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

        RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(runKey);

        if (enable)
        {
            if (startupKey.GetValue("ZanNews") == null)
            {
                startupKey.Close();
                startupKey = Registry.CurrentUser.OpenSubKey(runKey, true);
                startupKey.SetValue("ZanNews", "\"" + Application.ExecutablePath + "\"");
                startupKey.Close();
            }
        }
        else
        {
            startupKey = Registry.CurrentUser.OpenSubKey(runKey, true);
            startupKey.DeleteValue("ZanNews", false);
            startupKey.Close();
        }
    }

Although the entry appears in the registry and the Task Manager, the program doesn't start with Windows.

Before asking this question, I did prior research on StackOverflow and none of the solutions and code snippets proposed here and here worked. Either I got security and access error messages or the registry was written, but the program refused to start with the operating system. I see, however, that the questions above were asked in 2010 and 2011 and I am thinking that things changed since then.

Is there a way to make a program run at startup? I have Windows 10, Home Edition, version 1803 and .NET Framework 4.7.2 installed on Windows 10 April 2018 Update.

Later edit: other information:

  1. The value of Application.ExecutablePath is C:\\Users\\alexz\\OneDrive\\Programe\\C#/ZanScore/ZanScore/bin/Debug/ZanNews.exe";
  2. I tried removing the "#" character, with no luck;
  3. Screenshot of Registry Editor: Screenshot
  4. Screenshot of Task Manager (in Romanian): Screenshot
Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
  • How exactly are you **running** `SetStartup`? – mjwills Jan 31 '19 at 12:23
  • 1
    Please also include a screenshot in your question of a command prompt which has run `dir` in `C:\\Users\\alexz\\OneDrive\\Programe\\C#/ZanScore/ZanScore/bin/Debug/` (i.e. prove to us the folder exists, and the executable in it). – mjwills Jan 31 '19 at 12:44

1 Answers1

1

Doing some research, I found that it's a much better way to create a shortcut and place it in the Startup folder. More details are presented here and the code (which works and solves the problem) is:

        WshShell wshShell = new WshShell();
        IWshRuntimeLibrary.IWshShortcut shortcut;
        string startUpFolderPath =
          Environment.GetFolderPath(Environment.SpecialFolder.Startup);

        // Create the shortcut
        shortcut =
          (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(
            startUpFolderPath + "\\" +
            Application.ProductName + ".lnk");

        shortcut.TargetPath = Application.ExecutablePath;
        shortcut.WorkingDirectory = Application.StartupPath;
        shortcut.Description = "Launch My Application";
        // shortcut.IconLocation = Application.StartupPath + @"\App.ico";
        shortcut.Save();

To be able to use the above code, you need to include the IWshRuntimeLibrary namespace and add the Windows Script Host Object Model reference to the project.

Other reference is here

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34