1

Im trying to use this function but getting some errors:

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

The first error is on the line :

Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;

On the right hand of the line (Shell32.ShellLinkObject)folderItem.GetLink Im getting errors:

Error   2   One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

And on the line in the end:

Console.WriteLine(GetShortcutTargetFile(path));

The error is on the: GetShortcutTargetFile(path) the function was static but i removed the static and then i mgetting the error in the last line.

Error   4   An object reference is required for the non-static field, method, or property 'GatherLinks.Form1.GetShortcutTargetFile(string)

How can i fix all the errors and how to get all the shortucts that target files ?

2
  • The idea is to know to read a shortcut files. put them all in List for example.
    – Daniel Lip
    Commented Oct 25, 2012 at 8:36
  • 1
    Both errors are nicely solvable by Google...
    – TGlatzer
    Commented Oct 25, 2012 at 8:38

1 Answer 1

2

First error: add a reference to Shell32.dll in your project settings.

Second error: Where are you placing the two functions? It seems like you are trying to create the functions within a Form. That is why you can not access the "GatherLinks.Form1.GetShortcutTargetFile(string)". Move your code from the main function to the Form loaded event and you will be able to compile :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.