0

I am creating startup entry in window startup. If user deselects the entry using msconfig startup window, my app creates a duplicate entry. I need to either remove the existing entry if it existing there or skip creating duplicate. How can I do that?

My code to create startup entry is this:-

string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\" + "MyexeName.exe";

            if (System.IO.File.Exists(startUpFolderPath))
            {
                return;
            }

            WshShellClass wshShell = new WshShellClass();
            IWshRuntimeLibrary.IWshShortcut shortcut;
            shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(startUpFolderPath);
            shortcut.TargetPath = Application.ExecutablePath;
            shortcut.WorkingDirectory = Application.StartupPath;
            shortcut.Save();
user2319247
  • 88
  • 1
  • 9

1 Answers1

1

The entries are stored in the registry.

This is how you should add and remove entries:

using Microsoft.Win32;

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

    if (ShouldAdd)
        rk.SetValue(AppName, Application.ExecutablePath.ToString());
    else
        rk.DeleteValue(AppName, false);
}

here is a list of different entries:
https://stackoverflow.com/a/5394144/2027232

To get admin rights you need to add a manifest file to your app:
Ctrl+Shift+A (add new item), then select (Aplication manifest file)

Open the manifest file and change the line:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

and press save.

More info: How to give my C# app administrative rights? manifest file

Community
  • 1
  • 1
string.Empty
  • 10,393
  • 4
  • 39
  • 67
  • For using this, is it required to run the app with admin privileges? – user2319247 Sep 11 '13 at 06:17
  • Is there any other option available as I don't want to give admin rights to the app. – user2319247 Sep 11 '13 at 08:58
  • The list you see in the msconfig is all registry entries and you can remove/add/edit them by editing the registry but only applications with admin rights can edit the registry. If you want to make an application run on startup you can just put it in the "C:\Users\Nicolas\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" folder but you will not be able to remove programs that start on startup from here as they not there, they in the registry. And to answer your question on if there is another way to get admin rights, yes there is... "right-click > Run as Administrator". – string.Empty Sep 11 '13 at 09:07
  • Okay, I understand this. So I should just add shortcut file at Environment.SpecialFolder.Startup folder and check for the file only(as I am doing already in my code) and there will be no entry in msconfig, is that right? – user2319247 Sep 11 '13 at 10:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37172/discussion-between-user2319247-and-nicolas-tyler) – user2319247 Sep 11 '13 at 12:22