0

I have the following code below. I can't seem to get it to work for adding a folder to favorites. If I change it to specialfolders.desktop, it creates a shortcut on the desktop.

private void buttonAddFav_Click(object sender, EventArgs e)
    {
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        string targetPath = listFolderResults.SelectedItem.ToString();
        var wsh = new IWshShell_Class();
        IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
            Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\\shorcut2.lnk") as IWshRuntimeLibrary.IWshShortcut;
        shortcut.TargetPath = targetPath;
        shortcut.Save();
    }

enter image description here

SeanDon
  • 83
  • 1
  • 12
  • what do you mean by 'favorites', can you upload a screen capture? – Lei Yang Apr 13 '17 at 00:54
  • the favorites section in windows explorer. I just added an image – SeanDon Apr 13 '17 at 01:00
  • which OS are you testing on? – Lei Yang Apr 13 '17 at 01:02
  • I'm using Windows 7 – SeanDon Apr 13 '17 at 01:11
  • can you have a try following this [social msdn](https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d65a7e5-1c02-4c18-bbc0-8d49340ca5f7/how-can-i-add-folders-to-favorites-in-window-explrer-programmatically-c?forum=csharpgeneral) – Lei Yang Apr 13 '17 at 01:29
  • 1
    Possible duplicate of [How do I programmatically add a folder to the user's Favorites (in Windows Explorer)?](http://stackoverflow.com/questions/4271759/how-do-i-programmatically-add-a-folder-to-the-users-favorites-in-windows-explo) – Rufus L Apr 13 '17 at 05:05

2 Answers2

3

Environment.SpecialFolder.Favorites represents the folder that contains your Internet Explorer favorites, which are located in the %USERPROFILE%\Favorites folder.

There is no Environment.SpecialFolder value that represents your Windows Explorer favorites, which are located in the %USERPROFILE%\Links folder.

To retrieve the path to the Links folder, you will have to query the Shell directly for the FOLDERID_Links path using either:

  1. PInvoke to call SHKnownFolderPath().

  2. COM interop to call IKnownFolderManager.GetFolder() and then IKnownFolder.GetPath().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I am still pretty new to C#. Could you write out the example for how to go about this for me? Thank you! – SeanDon Apr 13 '17 at 05:01
  • @SeanDon Check this answer: http://stackoverflow.com/questions/4271759/how-do-i-programmatically-add-a-folder-to-the-users-favorites-in-windows-explo – Rufus L Apr 13 '17 at 05:05
1

The below code ended up working for me. The answers below were helpful in figuring out I need to build around the %userprofile%\links, but %userprofile% gave me errors when saving. When I used the method below, it worked.

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        string targetPath = listFolderResults.SelectedItem.ToString();
        string shortcutPath = string.Format(@"C:\Users\{0}\Links",Environment.UserName);
        MessageBox.Show(shortcutPath);

        var wsh = new IWshShell_Class();
        IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
            shortcutPath + string.Format(@"\{0}.lnk",textFavName.Text)) as IWshRuntimeLibrary.IWshShortcut;
        shortcut.TargetPath = targetPath;
        shortcut.Save();
SeanDon
  • 83
  • 1
  • 12
  • 2
    This code is not safe since you made assumption that the location of the Links folder is located in C:\Users\. But this special folder location can be changed. Right click the Links folder in Windows Explorer then click the Location tab and enter the physical folder location for Links special folder. So your code will fail if the user has moved his Links folder to somewhere else, eg. a folder in different partition. – Han Apr 13 '17 at 05:39
  • Great hint! We may use Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Links", instead of static string. Thanks. – Lin Oct 31 '17 at 23:00