1

I'd like to get the target information from a shortcut file using my silverlight OOB app, so I'm going to make the following code to work in my silverlight OOB. It seems I have to used P/Invoke to use Shell32.dll, but I'm not sure how I can use Folder, FolderItem, and ShellLinkObject? Most references explain how I can use the functions in the .dll using P/invoke:( Please give me any comments or sample code/links:)

public string GetShortcutTargetFile(string shortcutFilename)
{
  string pathOnly = Path.GetDirectoryName(shortcutFilename);
  string filenameOnly = Path.GetFileName(shortcutFilename);

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

  return String.Empty; // Not found
}
rene
  • 41,474
  • 78
  • 114
  • 152
user689072
  • 134
  • 8

1 Answers1

0

I found a solution.

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

    dynamic shell = AutomationFactory.CreateObject("Shell.Application");
    dynamic folder = shell.NameSpace(pathOnly);
    dynamic folderItem = folder.ParseName(filenameOnly);
    if (folderItem != null)
    {
        dynamic link = folderItem.GetLink;
        return "\""+link.Path +"\"" + " " + link.Arguments;
    }

    return String.Empty; // Not found
}
rene
  • 41,474
  • 78
  • 114
  • 152