12

Possible Duplicate:
Get target of shortcut folder

For example, in C:\TEMP\ I have a shortcut called test.dll the shortcut will lead to the file name test.dll

I want to get from the shortcut only the path name to the file it self. So, I'm calling this function in another recursive function and put in this function each time another directory from my hard disk.

For example, the first directory is C:\TEMP then in C:\TEMP there is the shortcut file which I want to get the path only to the file. In C:\TEMP for the test I have now 3 files :

hpwins23.dat
hpwmdl23.dat
hpwmdl23.dat - Shortcut (C:\TEMP\hpwmdl23.dat)

So, what I want to get is the path name of the shortcut in this case its C:\TEMP

I tried to use this function:

public 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);
            if (folder == null)
            {
            }
            else
            {
                FolderItem folderItem = folder.ParseName(filenameOnly);
                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    return link.Path;
                }
            }
            return string.Empty;
        }

but when I'm using the function and its getting to a shortcut I'm getting exception error on the line:

Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink //The exception is: NotImplementedException: The method or operation is not implemented

What shoud I do to solve it ?

This is the full exception error message:

System.NotImplementedException was caught
Message=The method or operation is not implemented.
Source=GatherLinks
StackTrace:
at Shell32.FolderItem.get_GetLink()
at GatherLinks.Form1.GetShortcutTargetFile(String shortcutFilename) in
D:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 904
at GatherLinks.Form1.offlinecrawling

Community
  • 1
  • 1
user1741587
  • 769
  • 2
  • 6
  • 27
  • This user is most likely asking to resolve a symbolic link (not a shortcut as in .lnk), in which case this code will fail. You need to use 'GetFinalPathNameByHandle()' with PInvoke. Sample code can be found in this [entry here](http://chrisbensen.blogspot.com/2010/06/getfinalpathnamebyhandle.html). – ykay May 21 '14 at 21:07
  • This is a very old question now, but still relevant. It is not a duplicate as indicated. The duplicate link says to use "shell32" but does not address the issue of attempting to use it and getting the error that the OP cites above. I am currently getting this error after following all of the instructions in the linked duplicate. Code compiles and runs, but it has the "not implemented" exception above. I have not yet found a reference on the internet that describes a solution to this specific problem. – lurker May 05 '18 at 14:40

1 Answers1

41

To get the target of a shortcut (.lnk file extension) you'll need first to have the following COM object: Windows Script Host Object Model

Then, you may use WshShell (or WshShellClass) and IWshShortcut interfaces to get the target of a shortcut

Example

            string linkPathName = @"D:\Picrofo Autobot.lnk"; // Change this to the shortcut path

            if (System.IO.File.Exists(linkPathName))
            {
             // WshShellClass shell = new WshShellClass();
                WshShell shell = new WshShell(); //Create a new WshShell Interface
                IWshShortcut link = (IWshShortcut)shell.CreateShortcut(linkPathName); //Link the interface to our shortcut

                MessageBox.Show(link.TargetPath); //Show the target in a MessageBox using IWshShortcut
            } 

Thanks,
I hope you find this helpful :)


You may try the following steps to add Windows Script Host Object Model to your project

  • Under Solution Explorer, Right-click your project name and select Add Reference
  • Select the tab COM from the pop-up Window
  • Under Component Name, select Windows Script Host Object Model
  • Click on OK
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • 2
    I tried to read lnk-shortcut from following location: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\shortcut.lnk and I get COMException: from HRESULT: 0x80020009 (DISP_E_EXCEPTION). My OS: Win 8.1 x64 – Anatolii Humennyi Apr 21 '14 at 10:27
  • 1
    Additional information about adding reference is so useful. I came here from https://blez.wordpress.com/2013/02/18/get-file-shortcuts-target-with-c/ and also there is another solution over there. – Furkan Ekinci Apr 10 '20 at 17:28