Get target of shortcut folder

27,740

Solution 1

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));
        }
    }
}

Solution 2

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);
  }
}

Solution 3

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.

Share:
27,740

Related videos on Youtube

Phu Minh Pham
Author by

Phu Minh Pham

Updated on September 05, 2021

Comments

  • Phu Minh Pham
    Phu Minh Pham over 2 years

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

  • Phu Minh Pham
    Phu Minh Pham about 12 years
    Thanks! Now I just need to find out how to look for a shortcuf folder programmaticaly :)
  • swdev
    swdev about 10 years
    I work on Windows 7 64 bit, and it fails in finding shortcut to VirtualBox installed in C:\Program Files\Oracle\VirtualBoxOSX. It kept returning the target to be in C:\Program Files (x86)\Oracle\VirtualBoxOSX\VirtualBox.exe. Any explanation on this behaviour?
  • yancyn
    yancyn over 7 years
    Using Shell32 with this method only allow in single thread application. You have to include [STAThread] in main entry point. You will get exception in unit testing with this code because unit testing is not allow STAThread. see answer. There is also solution to run this code in unit testing see haacked.com/archive/2014/11/20/xunit-and-sta
  • Admin
    Admin over 7 years
    OP asked how to get the target directory of the shortcut, not how to check if it is a shortcut.
  • Dai
    Dai about 4 years
    This only works for trivial *.lnk files - it won't work for *.lnk files created by Windows Installer (which don't point to files anyway) as well as certain other kinds of *.lnk files that don't store the path as a single string - unfortunately the binary file format is kinda complex: docs.microsoft.com/en-us/openspecs/windows_protocols/ms-shll‌​ink/…
  • jeffld
    jeffld over 3 years
    Windows 10 is a piece of garbage. This is probably why there are so many problems with links on Windows 10.
  • Zyo
    Zyo over 2 years
    Didn't work in my case, the function already return null on my .lnk files.
  • cursorrux
    cursorrux over 2 years
    Please explain your solution. Answers which do not have an explanation and are only code get flagged as low effort.
  • Admin
    Admin over 2 years
    Please add further details to expand on your answer, such as working code or documentation citations.