Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to get my win app to have an option to start automatically, by adding it to the startup menu. The user has an option to delete the startup and to add it.

I have the following code which works with Windows 7 but gives an error with windows XP. All I can find is that XP uses a different version of shell.

Can anyone tell me how I can get this working on XP please?

C#
private void chkStartUp_CheckedChanged(object sender, EventArgs e)
{
    if (chkStartUp.Checked == true)
    {
        WshShellClass wshShell = new WshShellClass();
        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 MyApp";
        // shortcut.IconLocation = Application.StartupPath + @"\App.ico";
        shortcut.Save();

    }
    else
    {
        string startUpFolderPath =  Environment.GetFolderPath(Environment.SpecialFolder.Startup);

        DirectoryInfo di = new DirectoryInfo(startUpFolderPath);
        FileInfo[] files = di.GetFiles("*.lnk");
        foreach (FileInfo fi in files)
        {
            if (fi.ToString() == Application.ProductName + ".lnk")
            {
                string shortcutTargetFile = GetShortcutTargetFile(fi.FullName);
                System.IO.File.Delete(fi.FullName);
            }
        }

    }
}
Posted
Updated 20-May-12 13:18pm
v2

1 solution

I suggest you to forget manipulating the startup menu. You can use it theoretically, but this is not the way nowadays. Use the registry instead: http://www.geekpedia.com/tutorial151_Run-the-application-at-Windows-startup.html[^]. You just need to add a checkable option somewhere and manipulate your registry entry.
 
Share this answer
 
Comments
Clifford Nelson 21-May-12 0:31am    
Good find. I knew that had to find the registry location, and a project that does it is even better.
milenalukic 21-May-12 16:58pm    
Great thanks. That is even easier than I was expecting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900