Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get target of shortcut folder

How do you get the directory target of a shortcut folder? I've search everywhere and only finds target of shortcut file.

like image 504
Phu Minh Pham Avatar asked Feb 23 '12 13:02

Phu Minh Pham


People also ask

How do I find the target of a shortcut?

Windows will open the folder and highlight the original file. You can see the folder path where the file is located in the top of the Windows Explorer window. Click the Shortcut tab, and view the Target field to see where the shortcut points to.

How do I find the target of a shortcut in Windows 10?

Right-click the shortcut to the file or folder. Select Properties from the context menu. Go to the 'Shortcut' tab. Look for the Target field (not Target location).

How do I find a folder shortcut?

Open Drive. Right-click a file or folder. Click Add shortcut to Drive. Select the location for the shortcut.

What is shortcut target?

Under most circumstances, shortcut targets are applications or batch files. You simply provide the full path to the application or batch file in the Target property. However, after entering the target, if you leave the Shortcuts view and then return to it, you find that the target has changed.


3 Answers

I think you will need to use COM and add a reference to "Microsoft Shell Control And Automation", as described in this blog post:

Here's an example using the code provided there:

namespace Shortcut
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using Shell32;

    class Program
    {
        public static string GetShortcutTargetFile(string shortcutFilename)
        {
            string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

            Shell shell = new Shell();
            Folder folder = shell.NameSpace(pathOnly);
            FolderItem folderItem = folder.ParseName(filenameOnly);
            if (folderItem != null)
            {
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                return link.Path;
            }

            return string.Empty;
        }

        static void Main(string[] args)
        {
            const string path = @"C:\link to foobar.lnk";
            Console.WriteLine(GetShortcutTargetFile(path));
        }
    }
}
like image 192
Luke Girvin Avatar answered Oct 04 '22 09:10

Luke Girvin


In windows 10 it needs to be done like this, first add COM reference to "Microsoft Shell Control And Automation"

// new way for windows 10
string targetname;
string pathOnly = System.IO.Path.GetDirectoryName(LnkFileName);
string filenameOnly = System.IO.Path.GetFileName(LnkFileName);

Shell shell = new Shell();
Shell32.Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null) {
  Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
  targetname = link.Target.Path;  // <-- main difference
  if (targetname.StartsWith("{")) { // it is prefixed with {54A35DE2-guid-for-program-files-x86-QZ32BP4}
    int endguid = targetname.IndexOf("}");
    if (endguid > 0) {
      targetname = "C:\\program files (x86)" + targetname.Substring(endguid + 1);
  }
}
like image 45
EJ Thayer Avatar answered Oct 04 '22 11:10

EJ Thayer


If you don't want to use dependencies you can use https://blez.wordpress.com/2013/02/18/get-file-shortcuts-target-with-c/ But lnk format is undocumented, so do it only if you understand risks.

like image 21
Suhan Avatar answered Oct 04 '22 09:10

Suhan