Results 1 to 5 of 5

Thread: Windows Shortcuts

  1. #1
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    2,413

    Windows Shortcuts

    Is there a way to get the target of a Windows Shortcut (.lnk) without having to reference Shell32?

    If not, would having the Interop.Shell32.dll in my plugin folder cause any problems?

  2. #2
    Join Date
    Dec 2004
    Location
    West Yorkshire, UK
    Posts
    4,497
    There are a few sites out there which have the .LNK format details as a result of reverse-engineering but it looks fairly complex to parse.

    I can't think of any reason why using Interop.Shell32.dll would be a problem.

    Not sure if you've already been playing with it but I found this along the way...
    Code:
    public string GetShortcutTargetFile(string shortcutFilename)
    {
    	string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
    	string filenameOnly = System.IO.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;
    		return link.Path;
    	}
    	return ""; // not found
    }
    Cheers,
    Brian

  3. #3
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    2,413
    Quote Originally Posted by bgowland View Post
    Not sure if you've already been playing with it but I found this along the way...
    Code:
    public string GetShortcutTargetFile(string shortcutFilename)
    {
    	string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
    	string filenameOnly = System.IO.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;
    		return link.Path;
    	}
    	return ""; // not found
    }
    Thanks Brian - I found that too and that's exactly what I'm currently using - I was just wondering if there was another way...

    Alex

  4. #4
    Join Date
    Dec 2004
    Location
    West Yorkshire, UK
    Posts
    4,497
    Quote Originally Posted by ACTCMS View Post
    Thanks Brian - I found that too and that's exactly what I'm currently using - I was just wondering if there was another way...
    You could probably do it with [DllImport("shell32")] and by defining the individual methods you need using
    Code:
    private static extern <return-type> <method-name>(<params>);
    but I suspect the Interop.Shell32.dll is probably just a wrapper class which does this for you.

    Cheers,
    Brian

  5. #5
    Join Date
    May 2006
    Location
    Canada
    Posts
    21,540
    Here's a BinaryReader version http://thelightness.blogspot.ca/2005...rget-in-c.html only because you said you were looking for alternatives.

    Martin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •