Extracting Path from a Shortcut file...

B2Ben

Well-known member
Joined
Aug 17, 2006
Messages
52
Programming Experience
Beginner
Hey all -

I'm trying to make my application more robust by allowing files to be dragged-and-dropped onto the executable, thus opening the dropped files when the program launches.

This is working well so far. I'm getting the file path(s) from the command line arguments and processing them.

The problem comes with windows shortcuts... the ".lnk" files. If I drag a shortcut to a file onto my un-launched app, the path I get is the path of the shortcut file (c:\somefolder\someshortcut.lnk)

It seems to me that there should be some simple function in the framework to help me translate this into the path of the linked file, but I'm having trouble finding it. Web/forum searches haven't turned up much either.

Any help would be appreciated. Thanks.
 
TargetPath property

This is how I create a shortcut. I think you could do something similar and just not save it

VB.NET:
    Dim oShortCut As IWshRuntimeLibrary.IWshShortcut
    Dim oWshShell As New WshShellClass

      oShortCut = oWshShell.CreateShortcut(sMenuPath & shortcutName & ".lnk")
      With oShortCut
        .IconLocation = shortcutFile & ",0"
        .TargetPath = shortcutFile
        .WindowStyle = 1
        .Description = shortcutName
        .WorkingDirectory = sTargetFolder
        .Arguments = shortcutArguments
        .Save()
      End With

Instead try:

VB.NET:
   Dim oShortCut As IWshRuntimeLibrary.IWshShortcut
   Dim oWshShell As New WshShellClass

   oShortCut = oWshShell.CreateShortcut([Shortcut file path])
   shortcutFile = oShortCut.TargetPath
Hope it helps
Mike
 
Last edited by a moderator:
Thanks, that works very well! I'll have to file that one away for future use.

However, this is a pretty simple app, with no external DLL references. I'd like avoid using the script host DLL if I can, and keep the app self-contained in one EXE. If anyone else knows how I can do this, let me know.
 
Thanks, JohnH... that did the trick. Some of the vb files triggered warnings in VS2005, mostly for a depricated variable type or something, but nothing bad enough to prevent me from compiling. For my purposes (getting the path) it worked great.
 
Back
Top